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."

但是....如果我在 round_number 中包含一行 raw_input 代码以让玩家确定轮数,然后将 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。用 int() 包装 raw_input 函数调用:

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