简体   繁体   中英

How do I accept yes/no input from user and continue notifying them if they select no

I hope to accept user's input as yes/no, if user input yes, the code will continue run the avaliable code, it user input no, the code will notifying user again and code will not run until user input 'yes' finally

Here is the brief introdcution:

# Here is part1 of code


a = input('Have you finished operation?(Y/N)')

if a.lower()  == {'yes','y' }
    # user input 'y', run part2 code

if a.lower() == {'no', 'n'}
    # user input no
    # code should notifying user again "'Have you finished operation?(Y/N)'"  
    # part2 code will run until user finally input 'y'

if a.lower() !={'yes','y', 'no', 'n'}:
    # user input other words
    # code should notifying user again "'Have you finished operation?(Y/N)'" 


# here is part2 of code

Do you have any ideas on how to solve this problem, I would appreciate if you can provide some suggestions

Update

here is the code I'm tring

yes = {'yes','y',}
no = {'no','n'}

print(1)
print(2)
while True:
a = input('Have you create ''manually_selected_stopwords.txt'' ???(Y/N)''')
if a.lower().split() == yes:
    break
if a.lower().split() == no:
    continue

print(3)
print(4)  

When I run it, it shows as follows, the first time I tried input 'n' and then 'y', this will always notifying me, even I type into 'y' and will not print 3 and 4

1
2
Have you create manually_selected_stopwords.txt ???(Y/N)>? n
Have you create manually_selected_stopwords.txt ???(Y/N)>? y
Have you create manually_selected_stopwords.txt ???(Y/N)>? y
Have you create manually_selected_stopwords.txt ???(Y/N)>? y
Have you create manually_selected_stopwords.txt ???(Y/N)

Some of your tests are unnecessary. You have the same outcome if you type "no" as typing "marmalade".

# Here is part1 of code

while True:
    # Calling `.lower()` here so we don't keep calling it
    a = input('Have you finished operation?(Y/N)').lower()

    if a == 'yes' or a == 'y':
        # user input 'y', run part2 code
        break


# here is part2 of code
print('part 2')

EDIT:

If you want to use a set rather than or then you can do something like this:

if a in {'yes', 'y'}:

You are comparing the result of input() (a string) with a set of strings for equality. They are never equal.

You can use in to check if your input is in the set:

Linked dupe asking-the-user-for-input-until-they-give-a-valid-response applied to your problem:

# do part one
yes = {"y","yes"}
no = {"n","no"}

print("Doing part 1")
while True:
    try:
        done = input("Done yet? [yes/no]")
    except EOFError:
        print("Sorry, I didn't understand that.")
        continue

    if done.strip().lower() in yes:
        break # leave while
    elif done.strip().lower() in no:
        # do part one again
        print("Doing part 1")
        pass
    else:    
        # neither yes nor no, back to question
        print("Sorry, I didn't understand that.")

print("Doing part 2")

Output:

Doing part 1
Done yet? [yes/no]  # adsgfsa
Sorry, I did not understand that.
Done yet? [yes/no]  # sdgsdfg
Sorry, I did not understand that.
Done yet? [yes/no]  # 23452345
Sorry, I did not understand that.
Done yet? [yes/no]  # sdfgdsfg
Sorry, I did not understand that.
Done yet? [yes/no]  # no
Doing part 1
Done yet? [yes/no]  # yes
Doing part 2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM