0

「スコア」が特定の数値に達すると停止する単純な while ループを作成しようとしています。

「ラウンド」の数を3に設定すると、以下で機能させることができました。

var_1 = 0
var_2 = 0

while var_1 < 3 and var_2 < 3:
    choice = raw_input("Choose apple or orange.")
    if choice.lower() == 'a' or choice.lower() == 'apple':
        var_1 += 1
        print "You like apples, huh?"
        print "Apples: " + str(var_1) + "; Oranges: " + str(var_2)
    elif choice.lower() == 'o' or choice.lower() == 'orange':
        var_2 += 1
        print "You like oranges, huh?"
        print "Apples: " + str(var_1) + "; Oranges: " + str(var_2)

print "Game over."
if var_1 == 3:
    print "Apples are more popular."
elif var_2 == 3:
    print "Oranges are more popular."

しかし.... raw_input コードの行を round_number に含めて、プレーヤーにラウンド数を決定させ、while 条件をその変数に設定すると、機能しません。

var_1 = 0
var_2 = 0
round_number = raw_input("How many rounds would you like to play?")
while var_1 < round_number and var_2 < round_number:
    choice = raw_input("Choose apple or orange.")
    if choice.lower() == 'a' or choice.lower() == 'apple':
        var_1 += 1
        print "You like apples, huh?"
        print "Apples: " + str(var_1) + "; Oranges: " + str(var_2)
    elif choice.lower() == 'o' or choice.lower() == 'orange':
        var_2 += 1
        print "You like oranges, huh?"
        print "Apples: " + str(var_1) + "; Oranges: " + str(var_2)

print "Game over."
if var_1 == round_number:
    print "Apples are more popular."
elif var_2 == round_number:
    print "Oranges are more popular."

私は何を間違っていますか?

4

1 に答える 1

3

int にキャストする必要があります。raw_input 関数呼び出しを int() でラップします。

round_number = int(raw_input("How many rounds would you like to play?"))
于 2013-11-04T17:48:13.507 に答える