user2953512
user2953512

Reputation: 1

while loop for number of rounds in a game

I'm trying to create a simple while loop that will stop after the "score" reaches a certain number.

I was able to make it work below when I set the number of "rounds" to 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."

BUT....if I include a line of raw_input code to round_number to have the player determine the number of rounds and then set the while conditions to that variable, it doesn't work:

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

What am I doing wrong?

Upvotes: 0

Views: 437

Answers (1)

sheba
sheba

Reputation: 850

You need to cast it to int. wrap the raw_input function call with int():

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

Upvotes: 3

Related Questions