'''python
from T123_P2_func import add_books def print_menu(commandmenu:dict)-> list:
menu_options = { 1: 'Command Line L)oad file', 2: 'Command Line A)dd book', 3: 'Command Line R )emove book', 4: 'Command Line F) ind book by title', 5: 'Command Line NC) 一個類別中的書籍數量', 6: 'Command Line CA) Categories for an author', 7: 'Command Line CB) 書名的類別', 8:'Command Line G)et book''\n' 'R)ate A)uthor P)ubliosher C)ategory',
}
對于 menu_options.keys() 中的鍵: print(key, '-', menu_options[key] ) print_menu(menu_options)
檔案 = 無
while True: userinput = (input('請輸入命令:'))
userinput = userinput.upper()
if userinput == 'L':
print('Book has been Loaded.')
file = books_to_list('')
elif userinput == 'A':
print('Book has been Added.')
elif userinput == 'R':
print('Book has been Removed.')
elif userinput == 'F':
print('Book has been Found.')
print(userinput)
word1 = (input('Press Q to Quit: '))
if word1 == 'Q':
break
'''
當我從另一個檔案中呼叫該函式時,該檔案中的每個函式都會運行,請問可能是什么問題,如果我想通過按字母來呼叫該函式該怎么辦?
uj5u.com熱心網友回復:
您是說匯入模塊時practicing.py運行的每個函式,還是運行主要部分中的代碼practicing.py?
如果外部模塊中的所有內容都包含在函式定義中,則匯入模塊應該不會對您在主程式中執行的操作產生任何影響。
如果您仍然希望通過運行該檔案來執行外部模塊主要部分中的代碼,那么要做的就是將模塊的主要部分包裝在一個函式中def main():,并將以下內容放在模塊的主要部分中(如果模塊被匯入,這將不會運行,只有當它直接運行時):
if __name__ == "__main__":
main()
uj5u.com熱心網友回復:
我看不到整個代碼。
但是,我認為您必須知道如何利用函式的引數。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/376177.html
