I'm a novice coder, trying to execute the below code in Python 3, IDE: Pycharm. For the line containing the code: opt_list[opt_choice] when I hover over that line of code, the message "Statement seems to have no effect" is popping up. When I execute the code, it shows me the Menu, takes the input. However, it just exits after that:
def remove_letter(): # Remove a selected letter from a string
print("Remove letter")
return
def num_compare(): # Compare 2 numbers to determine the larger
print("Number compare")
return
def print_string(): # Print the previously stored string
print("Printing the saved String:")
print(saved_string)
return
def calculator(): # Basic Calculator(addition, subtraction, multiplication, division)
print("Calculator")
return
def accept_and_store(): # Accept and store a string
print("Accept and store")
global saved_string
saved_string = str(input("Input Strings: "))
return
def main(): # menu goes here
opt_list = [accept_and_store,
calculator,
print_string,
num_compare,
remove_letter]
while True:
print(”SELECT OPTION:”)
print(”1.\tAccept and Store”)
print(”2.\tCalculator”)
print(”3.\tPrint Stored String”)
print(”4.\tNumber Comparision”)
print(”5.\tLetter Remover”)
print(”6.\tQuit”)
opt_choice = int(input(”SELECTION: ”))
opt_choice -= 1
opt_list[opt_choice]
return
main()
Please find the output for your reference:
SELECT OPTION:
1. Accept and Store
2. Calculator
3. Print Stored String
4. Number Comparision
5. Letter Remover
6. Quit
SELECTION: 3
Process finished with exit code 0
Replacing the line of code:
opt_list[opt_choice]
with
opt_list[opt_choice]()
Gave me the following output, but again exited after providing after displaying the output:
SELECT OPTION:
1. Accept and Store
2. Calculator
3. Print Stored String
4. Number Comparision
5. Letter Remover
6. Quit
SELECTION: 1
Accept and store
Input Strings: hello
Process finished with exit code 0
Expected Functionality: I'm expecting it to be a continuous loop (since in while, the condition to check is provided as "True", and it will be always true!), taking my inputs, without exiting, since there is no exit code written for the loop to exit.
I could see 2 issues.
There is no indentation for the 2 functions. Other is the Double quotes for the print function. It seems to be a different character. ” instead of " below the While function.
def main(): # menu goes here
opt_list = [accept_and_store,
calculator,
print_string,
num_compare,
remove_letter]
while True:
print("SELECT OPTION:")
print("1.\tAccept and Store")
print("2.\tCalculator")
print("3.\tPrint Stored String")
print("4.\tNumber Comparision")
print("5.\tLetter Remover")
print("6.\tQuit")
opt_choice = int(input("SELECTION: "))
opt_choice -= 1
opt_list[opt_choice]
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.