我如何使用while循环与变量列表

时间:2019-04-16 14:15:02

标签: python python-3.x

我想在用户作为输入给出的选择与选择存储为变量之间循环

filenames = ['file1.txt', 'file2.txt']
for filename in filenames:
    # ... see code above

1 个答案:

答案 0 :(得分:0)

您可以在while True循环内请求输入,然后在给出有效输入后从其中break退出输入:

action_map = {
    "A": str(Ac),
    "S": str(Sc),
    "D": str(Dc),
    "F": str(Fc)
}

while True:
    action_input = input("\n ")
    action_input1 = action_input.title()
    if action_input1 in action_map:
        print("\n Required Result is : " + action_map[action_input1])
        break
    else :
        print("\n Please enter from the choices given above")