TechnicalJay
TechnicalJay

Reputation: 21

How to ask a string again if the wrong answer is entered?

I'm trying to get a string to keep repeating if the answer is wrong. How would I go about doing this? The code I have is below which works but doesn't repeat the answer.

print("Hello there, what is your name?")
MyName = input()
print ("Nice to meet you " + MyName)
print ("What is 2 + 2?")

answer = input()
if answer is '4':
    print("Great job!")
elif answer != '4':
    print ("Nope! please try again.")
while answer != '4':
    print ("What is 2 + 2?")
    break

Upvotes: 1

Views: 15952

Answers (5)

jez
jez

Reputation: 15369

By now you've got more answers than you can handle, but here are another couple of subtleties, with notes:

while True:   # loop indefinitely until we hit a break...

    answer = input('What is 2 + 2 ? ')    # ...which allows this line of code to be written just once (the DRY principle of programming: Don't Repeat Yourself)

    try:
        answer = float(answer)    # let's convert to float rather than int so that both '4' and '4.0' are valid answers
    except:     # we hit this if the user entered some garbage that cannot be interpreted as a number...
        pass    # ...but don't worry: answer will remain as a string, so it will fail the test on the next line and be considered wrong like anything else

    if answer == 4.0:
        print("Correct!")
        break   # this is the only way out of the loop

    print("Wrong! Try again...")

Upvotes: 2

JazZ
JazZ

Reputation: 4579

Here is my two cents for python2 :

#!/usr/bin/env python


MyName = raw_input("Hello there, what is your name ? ")
print("Nice to meet you " + MyName)

answer = raw_input('What is 2 + 2 ? ')

while answer != '4':
    print("Nope ! Please try again")
    answer = raw_input('What is 2 + 2 ? ')

print("Great job !")

Upvotes: 0

Rafael
Rafael

Reputation: 3196

print('Guess 2 + 2: ')
answer = int(input())
while answer != 4:
    print('try again')
    answer = int(input())
print('congrats!')

I think this is the simplest solution.

Upvotes: 2

trincot
trincot

Reputation: 351298

You only need the wrong-answer check as loop condition and then output the "great job" message when the loop is over (no if is needed):

print("Hello there, what is your name?")
MyName = input()
print ("Nice to meet you " + MyName)
print ("What is 2 + 2?")
answer = input()
while answer != '4':
    print ("Nope! please try again.")
    print ("What is 2 + 2?")
    answer = input()

print("Great job!")

Upvotes: 1

Farhan.K
Farhan.K

Reputation: 3485

There are a couple of things wrong with your code. Firstly, you are only asking for the answer once at the moment. You need to put answer = input() in a while loop. Secondly, you need to use == instead of is:

print("Hello there, what is your name?")
MyName = input()
print ("Nice to meet you " + MyName)
print ("What is 2 + 2?")

answer = 0

while answer != '4':

    answer = input()
    if answer == '4':
        print("Great job!")
    else:
        print ("Nope! please try again.")

There are a number of ways you can arrange this code. This is just one of them

Upvotes: 3

Related Questions