繁体   English   中英

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

[英]”Statement seems to have no effect” when using While with True & a return for accessing a list

我是一个新手程序员,试图在Python 3,IDE中执行以下代码:Pycharm。 对于包含代码的行:当我将鼠标悬停在该行代码上时,将弹出消息“声明似乎无效”。 当我执行代码时,它将显示菜单,并接受输入。 但是,它只是在那之后退出:

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()

请找到输出以供参考:

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

替换代码行:

opt_list[opt_choice]

opt_list[opt_choice]()

给了我以下输出,但是在显示输出后再次退出:

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

预期的功能: 我希望它是一个连续的循环(因为在一段时间内,检查条件以“ True”的形式提供,并且永远是正确的!),我的输入没有退出,因为没有退出为退出循环编写的代码。

我可以看到2个问题。

这两个功能没有缩进。 其他是打印功能的双引号。 这似乎是一个不同的角色。 在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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM