循环如果elif声明

时间:2017-12-15 02:49:00

标签: python loops if-statement

从用户

获取价值
user_input = input("You decide: ")

将根据用户值打印某个短语的#function也会出现错误值的错误消息(输入错误值需要循环)

if user_input == 'A' or user_input == 'a':
    print("With Uncle Sam in your corner you shall not fail...or I could be wrong?\nEither way lets get that Clock rolling")
elif user_input == 'Q' or user_input == 'q':
    mess = ("Well to heck with ya then!")
    print(mess)
elif user_input == 'B' or user_input == 'b':
    print("May Mother Russia led you to a brighter future!\nLets begin shall we")
elif user_input == 'C' or user_input == 'c':
    print("Ahhhh..the lonley Red State....well all I can say is either they follow you or get regret ever doubting you!!\nBegin your reign")

else:
    print("Wrong Value!!!")

尝试循环语句,如果用户输入错误的值,它将重新开始。我陷入困境并接近将计算机扔掉,我知道有一个简单的解决办法,但我似乎无法找到它。

2 个答案:

答案 0 :(得分:1)

一个选项可能是检查用户输入是否在有效输入中,然后在while循环中提示,直到收到有效输入:

user_input = raw_input("You decide: ").lower()

while (user_input not in ['a', 'b', 'c', 'q']):
    print("Wrong Value!!!")
    user_input = raw_input("You decide: ").lower()

if user_input == 'a':
    print("With Uncle Sam in your corner you shall not fail...or I could be wrong?\nEither way lets get that Clock rolling")
elif user_input == 'q':
    mess = ("Well to heck with ya then!")
    print(mess)
elif user_input == 'b':
    print("May Mother Russia led you to a brighter future!\nLets begin shall we")
elif user_input == 'c':
    print("Ahhhh..the lonley Red State....well all I can say is either they follow you or get regret ever doubting you!!\nBegin your reign")
else:
    print("Unhandled case")

更简洁的打印方式是使用dict而不是if s

user_input = raw_input("You decide: ").lower()

while (user_input not in ['a', 'b', 'c', 'q']):
    print("Wrong Value!!!")
    user_input = raw_input("You decide: ").lower()

message_dict = {
    'a':"With Uncle Sam in your corner you shall not fail...or I could be wrong?\nEither way lets get that Clock rolling",
    'q':"Well to heck with ya then!",
    'b':"May Mother Russia led you to a brighter future!\nLets begin shall we",
    'c':"Ahhhh..the lonley Red State....well all I can say is either they follow you or get regret ever doubting you!!\nBegin your reign",
    'default': "Unhandled case"
}

print(message_dict.get(user_input, 'default'))

这也会更加炽热!

答案 1 :(得分:0)

您可以使用continuebreak命令创建直接循环:

while True:
    user_input = input("You decide: ")
    if user_input == ...
        print("With...
    elif user_input ==  ...
        print("Well...
    else:
        print("Wrong value!!!")
        continue
    break

这不是非常Pythonic,但会适合您当前的代码。

以下是该程序具有更多结构的另一个版本:

def query_and_response(prompt, responses):
    while True:
        try:
            return responses[input(prompt).lower()]
        except KeyError:
            print('Wrong value!!! valid values are {0}'.format(responses.keys()))

responses = {
    'a': 'With Uncle Sam in your corner you shall not fail...or I could be wrong?\nEither way lets get that Clock rolling',
    'b': 'May Mother Russia led you to a brighter future!\nLets begin shall we',
    'c': 'Ahhhh..the lonley Red State....well all I can say is either they follow you or get regret ever doubting you!!\nBegin your reign'
}
print query_and_response("You decide: ", responses)
相关问题