允许用户从带有for循环的菜单中选择选项

use*_*067 0 python for-loop

我正在为一个程序的一部分工作,该程序向用户呈现3个选项的菜单.我希望允许用户输入他们的菜单选项(1-3),之后菜单再次出现并允许用户输入另一个选项并重复此过程总共n次,用户也在菜单之前输入.

该程序只是连续3次打印菜单而不是单独的迭代,但我不知道如何解决这个问题.

n = int(input('Please enter the number of iterations:'))

for i in range(0,n):

    print('Enter 1 for choice 1\n')

    print('Enter 2 for choice 2\n')

    print('Enter 3 for choice 3\n')

    choice = int(input('Enter your choice:'))

    if (choice == 1):

    ....
    ....

    else:
        print('Invalid choice')
Run Code Online (Sandbox Code Playgroud)

Amb*_*ber 6

放置代码来处理循环内的选择:

n = int(input('Please enter the number of iterations:'))

for i in range(0,n):

    print('Enter 1 for choice 1\n')

    print('Enter 2 for choice 2\n')

    print('Enter 3 for choice 3\n')

    choice = int(input('Enter your choice:'))

    if (choice == 1):

        ....
        ....

    else:
        print('Invalid choice')
Run Code Online (Sandbox Code Playgroud)