Python: пока петли не остановятся - PullRequest
0 голосов
/ 25 апреля 2018

в настоящее время работает над проектом для 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 голосов
/ 25 апреля 2018

По сути, вы хотите вернуться к главному меню, поэтому вам нужно вызвать функцию, чтобы напечатать главное меню и принять пользовательский ввод внутри цикла 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()
0 голосов
/ 25 апреля 2018

Этот код решает вашу задачу?

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()
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...