簡體   English   中英

Python def / function 問題

[英]Python def / function issue

我想為餐廳制作菜單。 因為我是新人,所以我不想做很多選擇,所以我隨便找了個借口,但這不是我現在的問題。
當我執行def _menu_():代碼時,我只需要幫助,由於某種原因,它在 shell 中不起作用,它只是執行類似於<function _menu_ at 0x7fa592bf0430>操作,我真的不知道如何修復它。 ..

import sys
menu_choice = ('ok i will stay')

def _menu_():
    print('|---------------------------|')
    print('|Kids menu:                 |')
    print('| Chicken nuggets(G)        |')
    print('| Medium size MAC(G)        |')
    print('| EXTRA Fries (GF)          |')
    print('|*all menu comes with fries*|')
    print('|G = GLUTEN GF = GLUTENFREE |')
    print('|---------------------------|')

    
print('Owner:')
print ('hello, i am the owner of the restaurant, unfortunately we have been robbed and all our food is gone except for our catch of the day and our kids menu')
print('    ')
print('is it ok or you want to go to another restaurant (knowing that our prices reduced by 75%)')
print('write "ok i will stay" if you want to eat here and write "nevermind i will go see somewhere else" to go somewhere else')

menu_choice = ('ok i will stay')

if menu_choice:
    print('ok great so what will you take here is the menu')
    print (_menu_)
else:
    print('ok then, enjoy you expensive other restaurant')
    sys.exit 

(順便說一句,它還沒有完成但我可以做更多)

有幾個問題,我會努力解決的。


print (_menu_)

打印_menu_ function,你想這樣稱呼它:

_menu_()

不需要使用print()因為_menu_使用print本身。


sys.exit 

也應該像 function 一樣調用:

sys.exit()

menu_choice = ('ok i will stay')
if menu_choice:

if可能不是你想要的。 您應該將menu_choice定義為字符串,並進行比較:

menu_choice = 'Ok i will stay'

if menu_choice == 'Ok i wil stay'

沒有必要定義它兩次。


最終,代碼會變成類似

import sys

def _menu_():
    print('|---------------------------|')
    print('|Kids menu:                 |')
    print('| Chicken nuggets(G)        |')
    print('| Medium size MAC(G)        |')
    print('| EXTRA Fries (GF)          |')
    print('|*all menu comes with fries*|')
    print('|G = GLUTEN GF = GLUTENFREE |')
    print('|---------------------------|')

    
print('Owner:')
print ('hello, i am the owner of the restaurant, unfortunately we have been robbed and all our food is gone except for our catch of the day and our kids menu')
print('    ')
print('is it ok or you want to go to another restaurant (knowing that our prices reduced by 75%)')
print('write "ok i will stay" if you want to eat here and write "nevermind i will go see somewhere else" to go somewhere else')

menu_choice = 'ok i will stay'

if menu_choice == 'ok i will stay':
    print('ok great so what will you take here is the menu')
    _menu_()
else:
    print('ok then, enjoy you expensive other restaurant')
    sys.exit() 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM