Reputation: 558
Below is one part of a basic script that I have written whilst tinkering with Python. What I intend for this script to do is prompt me for the subject of my note, then asks me to confirm my selection. If for whatever reason I've selected the wrong subject I have incorporated a while loop that would repeat until my selection is correct. The result of the script would be that it would return the confirmed subject.
The problem which occurs when I execute the script is that when I reply with 'no' (or anything else which designates that I do not confirm that this is the correct selection) the terminal output spams me with the list of subjects repeatedly. The only way to terminate this is by KeyboardInterrupt.
How do I resolve this? I feel it may have something to do with the iteration statement in the while loop, or inadequate placement of the break statement.
Thank you for your your help.
def subject():
subject_dict = {1: 'Mathematics', 2: 'Computer Science', 3: 'English Literature & Language', 4: 'Philosophy', 5: 'Linguistics', 6: 'Art & Design'}
subject_prompt = ("\nSelect the subject of your note.\n")
print(subject_prompt)
for i in subject_dict:
subject_choices = str(i) + ". " + subject_dict[i]
print(subject_choices)
subject_prompt_input = input("\n> ")
x = int(subject_prompt_input)
confirmation = input("\nSo the subject of your note is" + " '" + subject_dict[x] + "'" + "?\n> ")
while confirmation in ['no', 'No', 'n', 'NO']:
print(subject_prompt)
for i in subject_dict:
subject_choices = str(i) + ". " + subject_dict[i]
print(subject_choices)
subject_prompt_input
confirmation
if confirmation in ['quit', 'stop', 'exit']:
quit()
if confirmation in ['Yes', 'YES', 'yes', 'y', 'Y']:
break
if confirmation in ['yes', 'y', 'YES', 'Y']:
selection = subject_dict[x]
return selection
Upvotes: 0
Views: 60
Reputation: 10631
Was your intention to keep looping until the user will press 'yes' or 'quit'?
If yes, you need to put the input inside the loop.
Change the while statement to while True
as you still don't have the confirmation variable.
while True:
confirmation = input("\nSo the subject of your note is" + " '" + subject_dict[x] + "'" + "?\n> ")
print(subject_prompt)
for i in subject_dict:
subject_choices = str(i) + ". " + subject_dict[i]
print(subject_choices)
subject_prompt_input
confirmation
if confirmation in ['quit', 'stop', 'exit']:
quit()
if confirmation in ['Yes', 'YES', 'yes', 'y', 'Y']:
break
if confirmation in ['yes', 'y', 'YES', 'Y']:
selection = subject_dict[x]
return selection
EDIT:
According to your question in the comments.
it's not related to insufficiency.
you could do:
confirmation = 'no'
while confirmation in ['no', 'No', 'n', 'NO']:
...
Although, it's ugly :)
Upvotes: 3