我是個人專案的新手,正在通過 python 終端開發圖書館圖書管理系統。
我遇到了一個已經卡住了一段時間的問題。
我正在嘗試讓我的代碼列印到控制臺中
- - - 主選單 - - -
- 所有書籍
- 報到
- 退房
- 抬頭
但相反,它只是一遍又一遍地給我“選擇選項”。我不確定我做錯了什么,因為我已經嘗試了很多次迭代,但我找不到解決方案。
源代碼:

終端輸出:

uj5u.com熱心網友回復:
將來嘗試將您的代碼直接粘貼到您的問題中,而不是作為影像。
option無論如何,嘗試移動以ie開頭的行:
option = input("Choose options: ")
進入mainMenu
函式內部(main_menu在下面嘗試重建您的嘗試中):
#
#
# Print menu
# Menu will include a list if menu options
# Create a main menu function that will control the main menu
# Then call functions as they are requested by the user
# Will require a dictionary using id's as the key and book info as values
#
#
import pprint
import os
# Books in the System
books = [
{"ID": 100001, "title": "Meditations", "author": "Marcus Aurelius", "year": 180},
{"ID": 100002, "title": "To Kill a Mockingbird", "author": "Harper Lee", "year": 1960},
{"ID": 100003, "title": "The Great Gatsby", "author": "F. Scott Fitzgerald", "year": 1925},
{"ID": 100004, "title": "Don Quixote", "author": "Miguel de Cervantes", "year": 1615},
{"ID": 100005, "title": "The Little Prince", "author": "Antoine de Saint-Exupery", "year": 180}
]
menu = " ------ Main Menu ------ \n\n 1. All Books \n 2. Check In \n 3. Check Out \n 4. Look Up \n"
def menu1():
pprint.pprint(books)
option1 = input("Type 0 to go back: ")
if option1 == "0":
os.system("clear")
return main_menu()
def main_menu():
print(menu)
option = input("Choose option: ")
if option == "1":
os.system("clear")
return menu1()
if __name__ == '__main__':
main_menu()
示例用法:
------ Main Menu ------
1. All Books
2. Check In
3. Check Out
4. Look Up
Choose option: 1
[{'ID': 100001,
'author': 'Marcus Aurelius',
'title': 'Meditations',
'year': 180},
{'ID': 100002,
'author': 'Harper Lee',
'title': 'To Kill a Mockingbird',
'year': 1960},
{'ID': 100003,
'author': 'F. Scott Fitzgerald',
'title': 'The Great Gatsby',
'year': 1925},
{'ID': 100004,
'author': 'Miguel de Cervantes',
'title': 'Don Quixote',
'year': 1615},
{'ID': 100005,
'author': 'Antoine de Saint-Exupery',
'title': 'The Little Prince',
'year': 180}]
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/435660.html
上一篇:函式的回傳型別可以在其范圍之外更改以不回傳任何值嗎?[復制]
下一篇:函式(x)如何在R中作業?
