1

I am coding a mock sign up page for a game or something or other, at the end of the code I want to confirm that the user entered data is correct. I do this by typing.

#User sign up page.

#Getting the user's information.
username = input ("Plese enter your first name here: ")
userage = input ("Please enter your age here: ")
userphoneno = input ("Please enter your home or mobile number here: ")

#Showing the inforamtion.  
print ("\nIs the following correct?\n")
print ("•Name:",username)
print ("•Age:",userage)
print ("•Phone Number:",userphoneno)

#Confirming the data. 
print ("\nType Y for yes, and N for no. (Non-case sensitive.)")
answer = input ("• ")
if answer == 'Y'or'y':
    print ("Okay, thank you for registering!")
    break 
else:
    #Restart from #Getting the user's information.? 

My problem arises in the last section of code. The program ends like normal when "Y or y" is entered, but I can't seem to work out how to let the user re enter their data if "N or n" is entered. I tried a While loop, which I'm guessing is the solution, but I couldn't seem to get it to work correctly.

Any help would be greatly appreciated. Thanks!

Reece
  • 549
  • 9
  • 23
  • 4
    Show us what you tried for the while loop? Also, `answer == 'Y'or'y'` will always evaluate to true. Take a look at [this](http://stackoverflow.com/questions/15112125/how-do-i-test-one-variable-against-multiple-values). – Morgan Thrapp Jan 13 '16 at 18:36

1 Answers1

1

You should use a while loop! Wrap the part that deals with user input with a function and then keep on calling that function if the user responds with no. By the way, you should use raw_input instead of input. For example:

#User sign up page.

#Getting the user's information.

def get_user_info():
    username = raw_input("Plese enter your first name here: ")
    userage = raw_input("Please enter your age here: ")
    userphoneno = raw_input("Please enter your home or mobile number here: ")

    #Showing the inforamtion.  
    print ("\nIs the following correct?\n")
    print ("Name:",username)
    print ("Age:",userage)
    print ("Phone Number:",userphoneno)
    print ("\nType Y for yes, and N for no. (Non-case sensitive.)")
    answer = raw_input("")
    return answer

answer = get_user_info()
#Confirming the data. 
while answer not in ['Y', 'y']:
    answer = get_user_info()

print ("Okay, thank you for registering!")
Mike Wu
  • 21
  • 2
  • You need to use `input`, not `raw_input`, as the OP (original poster) is using Python 3. (You can tell by the parentheses he's using for each `print` statement, because omitting these would print tuples instead.) – mbomb007 Jan 13 '16 at 19:10
  • Or if you like, you can add the while loop into the function get_user_info() to use recursion. – Mike Wu Jan 13 '16 at 19:19
  • `raw_input()` is not available in Python 3. Please change your code to use `input()`, since the poster is using Python 3. If you actually ran your own code, you'd see the printing is wrong if Python 2, or that `raw_input` isn't valid in Python 3. – mbomb007 Jan 13 '16 at 19:21