1

I am try to make a choose system. I want to use the GOTO command. But it actually doesn't works. I've already installed the goto-statement in pip.

from goto import with_goto
print('Welcome to * sell system.')
print('Choose the action:\n(1) Buy *\n(2) Contact us\n(3) Exit')
# Choose the action
while action := input('> '):
    if action in '1':
        break
    if action in '2':
        print('Contact Us\n* is a brand of *')
        break
    if action in '3':
        exit()
    print('Only 1 and 2 allowed.') 
# Choose the categories
if action in '1':
    print('Choose categories of merchandises.\n(1) *\n(2) *')
    print('Enter 0 to exit') 
# Choose the merchandise (A while in a if)   
    while category := input('> '):
        if category in '1':
            label .choose_merchandises
            print('Choose the merchandises.\n(1) *- *')
            while merchandises := input('> '):
                if merchandises in '1':
                    print('Title:*- *\nPrice: *\nAmount: *\nDetails:\nDo you wanna buy it?\n(y/n)')
                    while wannabuy := input('> '):
                        if wannabuy == "y":
                            print('Redirecting to pay...')
                            break
                        if wannabuy == "n":
                            goto .choose_merchandises                    
                        print('Only y or n allowed.')
                    print ('1')
        if category in '2':
            print('2')
            break
    print('Only 1 and 2 allowed.')

And it told me

Traceback (most recent call last):
  File "d:\*\*.py", line 18, in <module>
    label .choose_merchandises
NameError: name 'label' is not defined

How can I do. Appreciate.

Patch: I am a beginner of python. It is a choosing system in that people can do what they want to do, like contact us, buy some stuff, etc. At first, users choose the action like Buy some stuff, contact us. Users choose the categories of merchandises and then choose the merchandises.

Ruli
  • 2,592
  • 12
  • 30
  • 40
Reachead
  • 11
  • 3
  • 1
    Consider not using `goto` at all. [There's a good reason its not part of high-level languages like Python](https://stackoverflow.com/questions/3517726/what-is-wrong-with-using-goto). Your code is highly nested and difficult to read/understand/debug -- spaghetti code as described in the linked post. Stick to using conditionals, loops and functions instead (structured code). – costaparas Jan 17 '21 at 04:10
  • I'm actually is a beginner of python. I don't know how to use these commands at all. But I'm still learning. Thanks a lot. – Reachead Jan 17 '21 at 04:18
  • A finite state machine could be a suitable design pattern for what you are trying to achieve – Iain Shelvington Jan 17 '21 at 04:19
  • 1
    @reachead As a beginner, you should not use `goto` at all. In fact, many universities ban the use of `goto` for beginners (and rightly so, to prevent misuse) – costaparas Jan 17 '21 at 04:19
  • 1
    There are good reasons why Python does not have a `goto` statement. Then someone implemented it as an on [April fool joke](http://entrian.com/goto/). Don't be an April fool! ;-) – Klaus D. Jan 17 '21 at 04:22
  • Thank you. But, I mean... If I don't use goto. What command should I use? – Reachead Jan 17 '21 at 06:56
  • As mentioned, you can use loops, functions and conditionals -- structured code. – costaparas Jan 17 '21 at 10:17

0 Answers0