简体   繁体   English

当使用带有True的While和返回列表的返回值时,“声明似乎无效”

[英]”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.

相关问题 Python语句好像没有效果 - Python statement seems to have no effect 使用__getitem__方法,该语句似乎无效 - Statement seems to have no effect, using __getitem__ method 声明似乎对十六进制到十进制转换器没有影响 - Statement seems to have no effect hex to dec converter 如何解决“声明似乎无效”错误? - How can I fix 'Statement seems to have no effect' error? function中的参数好像没有效果? - parameter in function seems to have no effect? 如果在使用lambda,map和list时Boolean为True,如何返回以下位置的列表? - How to return a list of the following position if Boolean is True while using lambda, map, and list? 在 Qt 的插槽中使用任何“return”语句时会产生什么影响 - What effect will exist when using any 'return' statement inside Qt's slot 为什么 return True 必须在 if 语句和 for 循环之外? - Why does return True have to be outside the if statement and the for loop? 有没有办法在“for”循环中嵌套“if”语句,然后在新列表中返回“True”、“False”或“Unsure”? - Is there a way to nest an “if” statement in a “for” loop, to then return as “True”, “False”, or “Unsure” in a new list? 如果一个值等于值列表中的一个,是否有一种方法可以让 If 语句返回真 Python - Is There a Way for an If Statement to Return True if a Value is Equal to One of a List of Values Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM