我试图重复这个算法,并在我的代码中使用值,请帮助!!
进口随机def dice_roll(number):
if number == 12:
number = random.randint(1,12)
print(number)
return(number)
elif number == 6:
number = random.randint(1,12)
print(number)
return(number)
else:
number == 4
number = random.randint(1,12)
print(number)
return(number)
dice_roll(int(input("你想掷哪个骰子? ")--> ")))
是这样的吗?
import random
def dice_roll(number):
if number == 12:
number = random.randint(1,12)
print(number)
return(number)
elif number == 6:
number = random.randint(1,12)
print(number)
return(number)
else:
number == 4
number = random.randint(1,12)
print(number)
return(number)
while True:
dice_roll(int(input("Which dice would you like to roll? --> ")))
doRepeat=input('Go again?')
if doRepeat == 'no':
break
编辑: raw_input() was renamed to input().
你可以在3中使用"input()"。X,像旧版本一样,这个:
doRepeat=eval(input('Go again?'))
典型的方法是while True
循环,当用户退出时,您将break
,例如:
while True:
user_input = int(input("Which die would you like to roll (0 to quit)? "))
if user_input in (4, 6, 12):
dice_roll(user_input)
elif user_input:
print("Not an acceptable die.")
else:
break
请注意,您的dice_roll
函数目前总是掷12面骰子,并且可以简化:
def dice_roll(sides):
number = random.randint(1, sides)
print(number)
return number
(这也取消了对4,6和12的限制,因此您可能需要相应地修改输入循环)。