Guys have a question and cant seem to find the solution anywhere, most places are telling me to create a loop:
LINE1: user_name = input('what is your name')
LINE2: print('Ok, so your name is: ' + user_name + ', correct? Please type "Y" or "N" ')
LINE3: birth_date = input('Next, please enter your birthdate')
# I want to have this where if the user selects "Y" it carries on to the next line of code, LINE 3
If the user selects "N" I want it to go revert back to LINE1
Please let me know if there is a way to code this
Thanks!
Colin
Yes, loops are generally how you repeat things. Why is using a loop a problem?
Maybe like this:
def get_data(question):
check_data = 'N'
# you can only escape the while loop by entering Y
while not check_data == 'Y':
data = input(f'{question}')
check_data = input(f'Q: {question} A: {data} Is this correct? Enter Y if correct, anything else to repeat. ')
return data
# add questions you are interested in
questions = ['What is your name? ',
'When were you born? ',
'What is your shoe size? ',
'How many numbers before the decimal point in your bank balance? ']
# save the data for processing by Big Brother
info = {}
for question in questions:
info[question] = get_data(question)
You will see this:
Output:
What is your name? Peter
Q: What is your name? A: Peter Is this correct? Enter Y if correct, anything else to repeat.
What is your name? Peter Rabbit
Q: What is your name? A: Peter Rabbit Is this correct? Enter Y if correct, anything else to repeat. Y
When were you born? 1970-01-01
Q: When were you born? A: 1970-01-01 Is this correct? Enter Y if correct, anything else to repeat. Y
What is your shoe size? 15
Q: What is your shoe size? A: 15 Is this correct? Enter Y if correct, anything else to repeat.
What is your shoe size? 9
Q: What is your shoe size? A: 9 Is this correct? Enter Y if correct, anything else to repeat. Y
How many numbers before the decimal point in your bank balance? None
Q: How many numbers before the decimal point in your bank balance? A: None Is this correct? Enter Y if correct, anything else to repeat. Y
info looks like this:
Output:
{'What is your name? ': 'Peter Rabbit', 'When were you born? ': '868-01-11', 'What is your shoe size? ': '9', 'How many numbers before the decimal point in your bank balance? ': 'None'}
Maybe you want to check the format of the inputs too. That will get more complicated then!