0

当我键入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()是游戏逻辑的一部分并在其他地方定义,因此您可以忽略它们。

4

5 回答 5

1

您的问题是您正在将字符串与整数进行比较。

您需要先将输入转换为int

try:
    guess = int(prediction)
except ValueError:
    #Handle when a person enters an invalid number here
于 2013-07-10T03:33:31.513 回答
1

在第一个语句之后使用该elif语句。目前,您的代码

    if lobby == 'back':
        altvillagescene()
    else:
        print('Please type a valid option.')
        print('--------------------------------')
        altluckyrollgame()

正在检查是否 lobby == 'back' 并在所有其他情况下运行 else 。您可能不希望这样,因为 else 下的代码是与其他 if case 一起运行的。

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()
于 2013-07-10T03:35:01.880 回答
0

prediction返回的是input()一个字符串,所以所有的比较都失败了。尝试将值转换为整数:

prediction = int(input())
于 2013-07-10T03:33:21.903 回答
0

“else”块仅与最终的“if prediction == number1 + 1”匹配。这意味着如果猜到了正确的数字(或 number1 - 1 的情况),那么它仍然会运行最后的 else 块。

您需要更改代码以将“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
于 2013-07-10T03:34:51.987 回答
0

如果那里有三种不同的结构。你几乎肯定想要

if ...

elif ...

elif ...

else

于 2013-07-10T03:37:25.670 回答