繁体   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