My menu loop isn't working and I don't know why
Question:
I’m writing code for a project and I am just finishing up the menu, where there are 5 options and the user inputs a number to select said option. Here’s my code:
display_menu(True)
command = input("Enter a number (0 to exit): ")
while command != 0:
if command == 1:
namefile = input("Enter word list filename: ")
wordlist = make_list(namefile)
print('Word list is loaded.')
elif command == 2:
namefile = input('Enter movie review filename:')
moviedict = make_dict(namefile)
print('Movie reviews are loaded.')
elif command == 3:
searchword = input('Enter a word to search: ')
count1, score1 = search_word(searchword)
print(searchword + ' appears ' + count1 + ' times')
print('The average score for the reviews containing the word terrific is: ' + score1)
elif command == 4:
print_lists(list, list_scores)
elif command == 5:
pass
display_menu(True)
command = input("Enter a number (0 to exit): ")
it definitely prints the list but when I enter a command input it doesn’t actually work.
Answers:
command
is a string returned by input
, and you’re comparing to an int. You need to explicitly convert that string to an int.
command = int(input("Enter a number (0 to exit): "))
As @Chris said you must not compare string and integer.
You may convert the input to an integer or you can test for an alphabetic choice as in:
while command:
if command == "1"
...
and, why not, alphanumeric mnemonics:
while command:
if command in ("1", "f", "F")
...
elif command in ("2", "m", "M")
...
elif command in ("3", 'w', "W")
...
elif command in ('4', "l", "L")
...
I’m writing code for a project and I am just finishing up the menu, where there are 5 options and the user inputs a number to select said option. Here’s my code:
display_menu(True)
command = input("Enter a number (0 to exit): ")
while command != 0:
if command == 1:
namefile = input("Enter word list filename: ")
wordlist = make_list(namefile)
print('Word list is loaded.')
elif command == 2:
namefile = input('Enter movie review filename:')
moviedict = make_dict(namefile)
print('Movie reviews are loaded.')
elif command == 3:
searchword = input('Enter a word to search: ')
count1, score1 = search_word(searchword)
print(searchword + ' appears ' + count1 + ' times')
print('The average score for the reviews containing the word terrific is: ' + score1)
elif command == 4:
print_lists(list, list_scores)
elif command == 5:
pass
display_menu(True)
command = input("Enter a number (0 to exit): ")
it definitely prints the list but when I enter a command input it doesn’t actually work.
command
is a string returned by input
, and you’re comparing to an int. You need to explicitly convert that string to an int.
command = int(input("Enter a number (0 to exit): "))
As @Chris said you must not compare string and integer.
You may convert the input to an integer or you can test for an alphabetic choice as in:
while command:
if command == "1"
...
and, why not, alphanumeric mnemonics:
while command:
if command in ("1", "f", "F")
...
elif command in ("2", "m", "M")
...
elif command in ("3", 'w', "W")
...
elif command in ('4', "l", "L")
...