[英]”Statement seems to have no effect” when using While with True & a return for accessing a list
I'm a novice coder, trying to execute the below code in Python 3, IDE: Pycharm. 我是一个新手程序员,试图在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. 预期的功能: 我希望它是一个连续的循环(因为在一段时间内,检查条件以“ True”的形式提供,并且永远是正确的!),我的输入没有退出,因为没有退出为退出循环编写的代码。
I could see 2 issues. 我可以看到2个问题。
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. 在While函数下使用“”代替“”。
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]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.