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