简体   繁体   中英

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...

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_()

No need to use print() since _menu_ uses print itself.


sys.exit 

Should be called like a function too:

sys.exit()

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

The if probably does not way you want. You should define menu_choice as a string, and compare it:

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() 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM