0

と入力するplayと、 に乱数が割り当てられnumber1ます。予測を求められ、5 などの数字を入力します。5 を入力すると、elseステートメントではなく、常にステートメントが得られifます。print()生成された数を調べるために a も入れました。時々、私は 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

elif最初のステートメントの後にステートメントを使用します。現在、あなたのコード

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

ロビー == '戻る' かどうかをチェックし、それ以外の場合はすべて実行しています。else の下のコードは、他のすべての if ケースに加えて実行されるため、おそらくこれは必要ありません。

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

3 つの異なる if 構造があります。あなたはほぼ確実に欲しい

if ...

elif ...

elif ...

else

于 2013-07-10T03:37:25.670 に答える
0

「else」ブロックは、最後の「if Prediction == number1 + 1」とのみ一致します。これは、正しい数値が推測された場合 (または数値 1 - 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

predictionによって返されるinput()stringであるため、すべての比較は失敗します。値を整数にキャストしてみてください。

prediction = int(input())
于 2013-07-10T03:33:21.903 に答える