Python Forum
How to revert back to a previous line from user input
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to revert back to a previous line from user input
#1
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
Reply
#2
Yes, loops are generally how you repeat things. Why is using a loop a problem?
Reply
#3
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!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Βad Input on line 12 Azdaghost 4 916 Mar-25-2025, 02:40 PM
Last Post: deanhystad
  User input with while loops chizzy101010 2 4,788 Aug-25-2024, 06:00 PM
Last Post: chizzy101010
  Receive Input on Same Line? johnywhy 8 2,517 Jan-16-2024, 03:45 AM
Last Post: johnywhy
  WHILE LOOP NOT RETURNING USER INPUT AFTER ZerroDivisionError! HELP! ayodele_martins1 7 2,443 Oct-01-2023, 07:36 PM
Last Post: ayodele_martins1
  restrict user input to numerical values MCL169 2 1,806 Apr-08-2023, 05:40 PM
Last Post: MCL169
  user input values into list of lists tauros73 3 1,935 Dec-29-2022, 05:54 PM
Last Post: deanhystad
Information How to take url in telegram bot user input and put it as an argument in a function? askfriends 0 2,295 Dec-25-2022, 03:00 PM
Last Post: askfriends
Question Take user input and split files using 7z in python askfriends 2 2,274 Dec-11-2022, 07:39 PM
Last Post: snippsat
  Code won't break While loop or go back to the input? MrKnd94 2 1,898 Oct-26-2022, 10:10 AM
Last Post: Larz60+
Sad how to validate user input from database johnconar 3 3,949 Sep-11-2022, 12:36 PM
Last Post: ndc85430

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020