当我输入play
时,会为number1
分配一个随机数。它要求我进行预测,然后我输入一个数字,比如说5.输入5后我总是得到else
语句而不是if
语句。我甚至用print()
来找出生成的数字。有时我在1或1之内(游戏也允许在1之内),它仍然会重新指向else
语句。有人可以帮忙吗?感谢。
money = 1000000
def luckyrollgame():
global money
from random import choice
print('You are in the game lobby of Lucky Roll.')
print('Choose either \'rules,\' \'play,\' or \'back\'')
lobby = input()
if lobby == 'rules':
luckyrollgamerules()
if lobby == 'play':
die = [1, 2, 3, 4, 5, 6]
number1 = choice(die)
prediction = input('Please type your prediction number: ')
if prediction == number1:
print('Good job! You guessed right!')
money = money + 3
print('You now have ' + str(dollars) + 'dollars.')
if prediction == number1 - 1:
print('Good job! You guessed right!')
money = money + 3
print('You now have ' + str(dollars) + 'dollars.')
if prediction == number1 + 1:
print('Good job! You guessed right!')
money = money + 3
print('You now have ' + str(dollars) + 'dollars.')
else:
print('I\'m sorry. You didn\'t get the number right.')
print('The number was ' + str(number1) + '.')
money = money - 1
print('You now have ' + str(money) + 'dollars.')
print('--------------------------------------------------')
altluckyrollgame()
if lobby == 'back':
altvillagescene()
else:
print('Please type a valid option.')
print('--------------------------------')
altluckyrollgame()
* altluckyrollgame()
或altvillagescene()
等函数是游戏逻辑的一部分,并在别处定义,因此您可以忽略它们。
答案 0 :(得分:1)
您的问题是您正在将字符串与整数进行比较。
您需要先将输入转换为int
:
try:
guess = int(prediction)
except ValueError:
#Handle when a person enters an invalid number here
答案 1 :(得分:1)
在第一个语句后使用elif
语句。目前,您的代码
if lobby == 'back':
altvillagescene()
else:
print('Please type a valid option.')
print('--------------------------------')
altluckyrollgame()
正在检查lobby =='back'并在所有其他情况下运行else。您可能不希望这样,因为除了其他所有情况之外,其他地方的代码也会运行。
if x == 0: pass
elif x == 1: pass
else: pass
代码应如下所示
money = 1000000
def luckyrollgame():
global money
from random import choice
print('You are in the game lobby of Lucky Roll.')
print('Choose either \'rules,\' \'play,\' or \'back\'')
lobby = input()
if lobby == 'rules':
luckyrollgamerules()
elif lobby == 'play':
die = [1, 2, 3, 4, 5, 6]
number1 = choice(die)
prediction = input('Please type your prediction number: ')
######################### This too
try: prediction = int(prediction)
except ValueError: prediction = -10
#########################
if prediction == number1:
print('Good job! You guessed right!')
money = money + 3
print('You now have ' + str(dollars) + 'dollars.')
elif prediction == number1 - 1:
print('Good job! You guessed right!')
money = money + 3
print('You now have ' + str(dollars) + 'dollars.')
elif prediction == number1 + 1:
print('Good job! You guessed right!')
money = money + 3
print('You now have ' + str(dollars) + 'dollars.')
else:
print('I\'m sorry. You didn\'t get the number right.')
print('The number was ' + str(number1) + '.')
money = money - 1
print('You now have ' + str(money) + 'dollars.')
print('--------------------------------------------------')
altluckyrollgame()
elif lobby == 'back':
altvillagescene()
else:
print('Please type a valid option.')
print('--------------------------------')
altluckyrollgame()
答案 2 :(得分:0)
prediction
返回的input()
是字符串,因此所有比较都会失败。尝试将值转换为整数:
prediction = int(input())
答案 3 :(得分:0)
“else”块仅与最终的“if prediction == number1 + 1”匹配。这意味着如果猜到正确的数字(或数字1 - 1),那么它仍然会运行最后的其他块。
您需要更改代码以使用“elif”作为中间条件:
if prediction == number1:
pass # do the win
elif prediction == number1 - 1
pass # do the win
elif prediction == number1 + 1
pass # do the win
else:
pass # do the lose
答案 4 :(得分:0)
你有三种不同的if结构。你几乎肯定想要
if ...
elif ...
elif ...
else