0

I am trying to set up a simple user-input menu (non-gui) as follows:

If the user inputs 0:

  1. a file is loaded.
  2. a menu comes up with options 1-5 prompting for another user input from 1 to 5. Now, here, after the first user input, some processing is done. Then I would like to ask the user for a second user input. This should continue until the user enters 6 or larger.

Else (if the user does not enter 0) Print "Error exiting. Please restart program."

Here is what I have:

import csv

# Load input file:
choiceloading = input('Please enter 0 to load the input file: ')
if choiceloading == 0:
    with open('eggs.csv', 'rb') as csvfile:
    spamreader = csv.reader(csvfile, delimiter=' ', quotechar='|')
    for row in spamreader:
        print ', '.join(row)

print ("   M A I N - M E N U")
print ("1. Action 1")
print ("2. Action 2")
print ("3. Action 3")
print ("4. Action 4")
print ("5. Action 5")

# Get user input:
choice = raw_input('Enter choice [1-5] : ')

# Convert input (number) to int type:
choice = int(choice)

# Perform action based on menu-option selection by user:
if choice == 1:
    print ("Action 1...")
    #processing #1 is done here
elif choice == 2:
    print ("Action 2...")
    #processing #2 is done here
elif choice == 3:
    print ("Action 3...")
    #processing #3 is done here
elif choice == 4:
    print ("Action 4...")
    #processing #4 is done here
elif choice == 5:
    print ("Action 5...")
    #processing #5 is done here
else:
    print ("Invalid entry. You should choose 1-5 only. Program exiting.....please restart it and try again.")
else:
    print ("Invalid entry. You should choose 0 to load the file. Program exiting.....please restart it and try again.")

Currently, the program only accepts 1 user input. After processing, it then exits. ex. if the user enters 3, and processing of 3 is complete then the program will exit.

However, I would like to allow the user to enter 1,2,4,5 for processing of 1,2,4,5. Then they should enter >5 to exit.

Question: After processing, how can this code be changed to allow the user to enter the remaining outputs?

1 Answer 1

1

The easiest way is to add the processing in a loop, like so:

while True:
    # Get user input:
    choice = raw_input('Enter choice [1-5] : ')

    # Convert input (number) to int type:
    choice = int(choice)

    if choice == 1:
        print ("Action 1...")
        #processing #1 is done here
    elif choice == 2:
        print ("Action 2...")
        #processing #2 is done here
    elif choice == 3:
        print ("Action 3...")
        #processing #3 is done here
    elif choice == 4:
        print ("Action 4...")
        #processing #4 is done here
    elif choice == 5:
        print ("Action 5...")
        #processing #5 is done here
    else:
        print ("Invalid entry. You should choose 1-5 only. Program exiting.....please restart it and try again.")
        # Add sys.exit() (will have to add 'import sys' to top
    else:
        print ("Invalid entry. You should choose 0 to load the file. Program exiting.....please restart it and try again.")
        # Add sys.exit()
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. I see that the last else is not required. It does not seem to be working. I am able to cycle through all the options, but how do I exit the program completely? That last else was there if the user did not enter 0 to load the file (as they should), but it is not working correctly. Is there a way to exit if "choiceloading" is not equal to 0?
You need to add sys.exit() where you want to end the program and then at the top of the file add import sys. You could do it another way as well by putting that entire loop in a function, put a return statement where you want to exit, then call the function in your program.
Done. Thanks a lot. That worked. All questions here are answered.

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.