在循环开始时循环播放

时间:2020-05-19 10:06:52

标签: python

如何返回主菜单。这是我的代码。尚未完成。例如,如果我选择1,如何不终止程序就返回主菜单。

php artisan storage:link

5 个答案:

答案 0 :(得分:1)

只需简化代码并检查循环中的条件即可:

done = False
while not done:
    print('=========================================')
    print('  Aria\'s Pet Store Point of Sale System')
    print('=========================================')
    print('[1] Add Products to Cart')
    print('[2] Remove Products to Cart')
    print('[3] View Cart')
    print('[4] Generate Reciept')
    print('[5] Logout')
    print('[6] Exit')
    print('=========================================')
    choice = input('Choice: ')

    if choice == '1':
        pass
    elif choice == '2':
        pass
    elif choice == '3':
        pass
    elif choice == '4':
        pass
    elif choice == '5':
        pass
    elif choice == '6':
        exit(0)
    else:
        print('Invalid Input')

答案 1 :(得分:0)

默认情况下,您的代码返回到循环顶部。您想要实现什么?也许您希望在某些语句满足后将done设置为True,并且希望退出循环,否则它将无限期地重复循环。也许您打算将if语句放入循环中:

done = False
while not done:
    print('=========================================')
    print('  Aria\'s Pet Store Point of Sale System')
    print('=========================================')
    print('[1] Add Products to Cart')
    print('[2] Remove Products to Cart')
    print('[3] View Cart')
    print('[4] Generate Reciept')
    print('[5] Logout')
    print('[6] Exit')
    print('=========================================')
    choice = input('Choice: ')

    if choice == '1':
        #do some stuff
    elif choice == '2':
        pass
    elif choice == '3':
        pass
    elif choice == '4':
        pass
    elif choice == '5':
        pass
    elif choice == '6':
        exit(0)
    else:
        print('Invalid Input')

    print('Would you like to choose again?')
    print('[1] Press y to go back to main menu')
    print('[2] press any other key to exit')
    redo = input('redo?: ')
    if redo != 'y':
        done = True

答案 2 :(得分:0)

您可以将if-else逻辑推入缩进内。另外,您可能想使用while True:循环回到菜单;这将帮助您避免冗余逻辑。这是重构的代码。

while True:
    print('=========================================')
    print('  Aria\'s Pet Store Point of Sale System')
    print('=========================================')
    print('[1] Add Products to Cart')
    print('[2] Remove Products to Cart')
    print('[3] View Cart')
    print('[4] Generate Reciept')
    print('[5] Logout')
    print('[6] Exit')
    print('=========================================')
    choice = input('Choice: ')

    if choice == '1':
        pass
    elif choice == '2':
        pass
    elif choice == '3':
        pass
    elif choice == '4':
        pass
    elif choice == '5':
        pass
    elif choice == '6':
        exit(0)
    else:
        print('Invalid Input')

答案 3 :(得分:0)

您的if段不在while循环中,因此if段无法读取“ choice”变量的值。如果在while循环中移动它们,它将正常工作。

答案 4 :(得分:0)

根据您的其他解释,我将其修复。您应该在重复任务时使用“功能”(https://www.w3schools.com/python/python_functions.asp)。您也可以像我在第一个选项中所做的那样修改菜单选项。

def mainmenu(A_Cart):
    print('=========================================')
    print('  Aria\'s Pet Store Point of Sale System')
    print('=========================================')
    print(f'[1] {A_Cart}')
    print('[2] Remove Products to Cart')
    print('[3] View Cart')
    print('[4] Generate Reciept')
    print('[5] Logout')
    print('[6] Exit')
    print('=========================================')
    choice = input('Choice: ')
    return choice


done = False
while not done:
    choice = mainmenu('Add Products to Cart')
    if choice == '1':
        mainmenu('Murda - Eh Baba (prod. Rockywhereyoubeen)')
    elif choice == '2':
        pass
    elif choice == '3':
        pass
    elif choice == '4':
        pass
    elif choice == '5':
        pass
    elif choice == '6':
        exit(0)
    else:
        print('Invalid Input')