-1

Pythonで完了する文字推測ゲームがあります。ユーザーが選択できる文字は「a、b、c、および d」です。5 回試行する方法は知っていますが、正しい文字の 1 つを推測したときに、ループを抜け出してプレーヤーを祝福することはできません。

    g = 0
    n = ("a", "b", "c", "d")

    print("Welcome to the letter game.\nIn order to win you must guess one of     the\ncorrect numbers.")
    l=input('Take a guess: ');
    for g in range(4):

    if l == n:
        break

        else:
            l=input("Wrong. Try again: ")


    if l == n:
            print('Good job, You guessed one of the acceptable letters.')

    if l != n:
            print('Sorry. You could have chosen a, b, c, or d.')
4

2 に答える 2

0

これにより、ほとんどのコードが保持されますが、目的に合わせて再配置されます。

n = ("a", "b", "c", "d")
print('Welcome to the letter game. In order to win')
print('you must guess one of the correct numbers.\n')

guess = input('Take a guess: ');
for _ in range(4):
    if guess in n:
        print('Good job, You guessed one of the acceptable letters.')   
        break      
    guess = input("Wrong. Try again: ")
else:
    print('\nSorry. You could have chosen a, b, c, or d.')

ループ変数の値はあまり気にしません。これを明確にするため_に、変数の代わりに ' ' を使用します。

于 2013-10-01T01:24:28.593 に答える