Python:虽然循环不会停止

时间:2018-04-25 02:34:53

标签: python loops

目前正在开发一个python项目,我们必须开发一个POS系统。对于我们的菜单,它需要是一个循环。我们不是最好的"开发者"什么,但是会喜欢一些见解。我们需要菜单在用户完成功能后循环回主选项。

def main():
    mainMenu = 'Main Menu:  \n   1. Checkout  \n   2. Returns  \n   3. Additonal Items  \n   4. Item Reference'
    Option1 = "Inititiate Transaction:  \n    scan item  \n    remove item  \n    accept payment  \n    display receipt  \n    back"
    Option2 = "Returns: \n   initiating returns \n   refund money \n   remove item from transaction \n   back"
    Option3 = "Additonal Items: \n   initiating transaction \n   enter new item information \n   display item confirmation \n   back"
    Option4 = "Item Reference: \n   lookup \n   scan item for info \n   display item info \n   back"



    print(mainMenu)
    userInput=(input("Please select a number: "))
    while userInput!="0":
        if userInput=="1":
            print(Option1)
            break
        elif userInput=="2":
            print(Option2)
            break
        elif userInput=="3":
            print(Option3)
            break
        elif userInput=="4":
            print(Option4)
            break
        elif userInput=="0":
            print(mainMenu)
            break


main()

2 个答案:

答案 0 :(得分:1)

基本上,您想要做的是循环回主菜单,因此您应该调用该函数来打印主菜单并在while循环中接受用户输入。

def main():
    mainMenu = 'Main Menu:  \n   1. Checkout  \n   2. Returns  \n   3. Additonal Items  \n   4. Item Reference'
    Option1 = "Inititiate Transaction:  \n    scan item  \n    remove item  \n    accept payment  \n    display receipt  \n    back"
    Option2 = "Returns: \n   initiating returns \n   refund money \n   remove item from transaction \n   back"
    Option3 = "Additonal Items: \n   initiating transaction \n   enter new item information \n   display item confirmation \n   back"
    Option4 = "Item Reference: \n   lookup \n   scan item for info \n   display item info \n   back"



userInput = 10
while userInput!="0":
    print(mainMenu)
    userInput=(input("Please select a number:"))
    if userInput=="1":
        print(Option1)
        #perform the task here
    elif userInput=="2":
        print(Option2)
        #perform the task here
    elif userInput=="3":
        print(Option3)
        #perform the task here
    elif userInput=="4":
        print(Option4)
        #perform the task here
main()

答案 1 :(得分:-1)

此代码是否解决了您的任务?

def main():
    mainMenu = 'Main Menu:  \n   1. Checkout  \n   2. Returns  \n   3. Additonal Items  \n   4. Item Reference'
    Option1 = "Inititiate Transaction:  \n    scan item  \n    remove item  \n    accept payment  \n    display receipt  \n    back"
    Option2 = "Returns: \n   initiating returns \n   refund money \n   remove item from transaction \n   back"
    Option3 = "Additonal Items: \n   initiating transaction \n   enter new item information \n   display item confirmation \n   back"
    Option4 = "Item Reference: \n   lookup \n   scan item for info \n   display item info \n   back"

    print(mainMenu)
    userInput=(input("Please select a number: "))
    while userInput != "0":
        if userInput=="1":
            print(Option1)
            print(mainMenu)
            userInput=(input("Please select a number: "))
        elif userInput=="2":
            print(Option2)
            print(mainMenu)
            userInput=(input("Please select a number: "))
        elif userInput=="3":
            print(Option3)
            print(mainMenu)
            userInput=(input("Please select a number: "))
        elif userInput=="4":
            print(Option4)
            print(mainMenu)
            userInput=(input("Please select a number: "))
    print("Exiting")

main()