简体   繁体   English

Python def / function 问题

[英]Python def / function issue

I wanted to make a menu for a restaurant.我想为餐厅制作菜单。 Because I am new, I wanted to make not a-lot of choices so I got a random excuse but thats not my problem right now.因为我是新人,所以我不想做很多选择,所以我随便找了个借口,但这不是我现在的问题。
I just need help as when I do my def _menu_(): code, for some reason it doesn't work in the shell it just does something like <function _menu_ at 0x7fa592bf0430> and I don't really know how to fix it...当我执行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 

(btw, it is not finished yet I can do a-lot more) (顺便说一句,它还没有完成但我可以做更多)

There are a few issues, Ill try to address them.有几个问题,我会努力解决的。


print (_menu_)

Prints the _menu_ function, you want to call it like so:打印_menu_ function,你想这样称呼它:

_menu_()

No need to use print() since _menu_ uses print itself.不需要使用print()因为_menu_使用print本身。


sys.exit 

Should be called like a function too:也应该像 function 一样调用:

sys.exit()

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

The if probably does not way you want. if可能不是你想要的。 You should define menu_choice as a string, and compare it:您应该将menu_choice定义为字符串,并进行比较:

menu_choice = 'Ok i will stay'

if menu_choice == 'Ok i wil stay'

There's no need to define it twice.没有必要定义它两次。


Eventually, the code would become something like最终,代码会变成类似

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