0

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
3
  • You will have to re-ask the user to update confirmation inside the loop. Commented Jan 22, 2017 at 12:02
  • 1
    As for your tests against possible input: use confirmation.lower(), so you only have to check against y or yes and n or no. Commented Jan 22, 2017 at 12:22
  • @RadLexus Thanks for the tip! Commented Jan 22, 2017 at 12:53

1 Answer 1

3

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 :)

Sign up to request clarification or add additional context in comments.

2 Comments

Yes that was my intention. Could you explain a little more behind why while confirmation in ['no', 'No', 'n', 'NO']: is insufficient for what I need to do?
I've added the explanation to the answer @seeker

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.