2

I'm reasonably new to Python. I wanted to know if I could use an input and ask a question like 'are you sure?', and if the answer is no to go back to the original input. I've got this so far:

variable = input("Make a choice between a, b, c or d. ")
while variable not in ("a","b","c","d"):
    variable = input("Make a correct choice. ")

if variable == "a":
    do things
if variable == "b":
    do other things
etc etc

I want to ask, after they have typed in their choice, are you sure about your choice? If they say yes, that's fine, carry on, but if they say 'no' I want to be able to go to the same input without typing the whole thing out again. Is there any way to do that?

AquaAvis
  • 55
  • 6
  • 2
    Not quite a dupe, but relevant: https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response – Patrick Haugh Jan 11 '18 at 18:18
  • Possible duplicate of [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – quamrana Jan 11 '18 at 18:23

5 Answers5

4

You could embed the bit that you want to repeat in a while True block that you break out of? For example:

while True
    answer = input("What is the correct answer, a, b, or c? ")
    check = input("Are you sure (y/n)? ")
    if check=="y" or check=="Y":
        break
Ari Cooper-Davis
  • 3,374
  • 3
  • 26
  • 43
  • Would it be better to use 'check.lower()' instead of checking if it's y or Y? Or does it not really make much difference? – AquaAvis Jan 11 '18 at 22:32
  • You certainly could do if you wanted, I did it for readability to make the code easier to understand, but `if check.lower()=="y":` would work perfectly too :) – Ari Cooper-Davis Jan 11 '18 at 22:39
1

Take the code you already have and wrap it in another while loop:

# loop forever until they confirm their choice
while True:
    variable = input("Make a choice between a, b, c or d. ")
    while variable not in ("a","b","c","d"):
        variable = input("Make a correct choice. ")
    confirm = input("You entered %s.  Is this correct?" % variable)
    if confirm == "yes":
        break
John Gordon
  • 29,573
  • 7
  • 33
  • 58
0

Something like this will work (though it's not the cleanest thing right now).

def prompt_for_something():
   variable = input("Make a choice between a, b, c or d. ")
   while variable not in ("a","b","c","d"):
       variable = input("Make a correct choice. ")

   confirm = input("Are you sure?")

   if confirm == "No": return False
   return variable

option = prompt_for_something()
while option == False:
   option = prompt_for_something()

if option == "a":
   do something
pushkin
  • 9,575
  • 15
  • 51
  • 95
0
ok=False
while not OK:
    variable = input("Make a choice between a, b, c or d. ")
    while variable not in ("a","b","c","d"):
        variable = input("Make a correct choice. ")
    ishesure=input("You chose {},  Are you sure?  (Y or N)".format(variable))
    if ishesure=="Y":
        ok=True

Should work. You surround everything by a while loop that will loop until the customer enters "Y" to your second question, that is asked once he entered a valid value for variable

WNG
  • 3,705
  • 2
  • 22
  • 31
0

It is not easy to have editable console output. If it is just for you, you can press the 'up' arrow key to go back to your last input, but if you want to embed it into the code it may be easier to use a proper GUI (i.e. tkinter) than what you are doing.

speedstyle
  • 142
  • 4
  • 11