0

I am trying to make a question repeat if the answer entered was incorrectly.

answer = raw_input("Please enter the capital of Canada")
Times = 0

if answer == "Ottawa":
    print "good you made " + str(Times) + " attempts"
    print "you know Canadian Geography"
else:
    Times = Times + 1
    print "Please take another Canadian Geography course."
martineau
  • 119,623
  • 25
  • 170
  • 301
Skipper
  • 3
  • 1

2 Answers2

0

You'll want a while loop.

Times = 0

while True:
    answer = raw_input("Please enter the capital of Canada")

    if answer == "Ottawa":
        print "good you made " + str(Times) + " attempts"
        print "you know Canadian Geography"
    else:
        Times = Times + 1
        print "Please take another Canadian Geography course."

This will create an infinite loop, so you might want some stopping criteria, like if the user answers incorrectly 10 times.

Times = 0
StopTimes = 10

while True:
    answer = raw_input("Please enter the capital of Canada")

    if answer == "Ottawa":
        print "good you made " + str(Times) + " attempts"
        print "you know Canadian Geography"
    else:
        Times = Times + 1
        print "Please take another Canadian Geography course."

    if Times > StopTimes:
        break
Eric Leung
  • 2,354
  • 12
  • 25
0

Similar to Erics answer but this loop breaks when they get it right.

Note for python3 use input instead of raw_input.

answer=raw_input("Please enter the capital of Canada:")

attempts=1
flag=True
while flag:
    if answer=="Ottawa":
        print("Good, you made %d attempts"%attempts)
        flag=False
    else:
        attempts += 1
        answer=raw_input("Please take another attempt, enter the capital of Canada:")
Jesse
  • 66
  • 2