我试图让函数骰子从 1-6 随机滚动。但是当我尝试从函数中添加不同的数字时,它说一和二是未定义的。我不确定如何解决这个问题,而且我对编码很陌生。
import random
def dice(name):
name = random.randint(1,6)
print name
dice('one')
dice('two')
dicesum = float(one) + float(two)
message = raw_input('guess the number. ')
if dicesum == message:
print "You guess right! You win!"
if dicesum != message:
print "You guess wrong! You lose!"
您可能想要one
并two
表示掷骰子返回的值:
import random
def roll_dice():
value = random.randint(1, 6)
print(value)
return value
roll_one = roll_dice()
roll_two = roll_dice()
dice_sum = roll_one + roll_two
guess = int(input('guess the number. ')) # cast to int so it can be compared to dice_sum
if dice_sum == guess:
print("You guess right! You win!")
else:
print("You guess wrong! You lose!")
我将变量名和函数名更改为我认为更好地表示它们是什么和做什么的。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句