首页 > 解决方案 > 当我在 while 循环中使用 GOTO 命令时它不起作用

问题描述

我正在尝试制作一个选择系统。我想使用 GOTO 命令。但它实际上不起作用。我已经在 pip 中安装了 goto 语句。

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

它告诉我

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

我能怎么做。欣赏。

补丁:我是python的初学者。它是一个选择系统,人们可以做他们想做的事情,比如联系我们,买东西等等。首先,用户选择像买东西,联系我们这样的动作。用户选择商品的类别,然后选择商品。

标签: pythongoto

解决方案


推荐阅读