同时循环游戏中的轮数

时间:2013-11-04 17:44:50

标签: while-loop

我正在尝试创建一个简单的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."

我做错了什么?

1 个答案:

答案 0 :(得分:3)

您需要将其强制转换为int。用int()包装raw_input函数调用:

round_number = int(raw_input("How many rounds would you like to play?"))