我怎么把它放进去?

时间:2017-01-17 16:15:08

标签: python loops while-loop

skill=input("What is your skill leavel ? E or C ? (Expert of casual): ")
if skill == "E":
    print ("Expert")
elif skill == "C":
    print ("Casual")

如何实现while循环,以便当用户输入E或C以外的答案时,它会再次询问?

4 个答案:

答案 0 :(得分:1)

Python中没有do-until个循环。常见的方法是while True

while True:
    skill=input("What is your skill leavel ? E or C ? (Expert of casual): ")
    if skill == "E":
        print ("Expert")
        break
    elif skill == "C":
        print ("Casual")
        break
    print("Invalid input, try again:")

替代:

valid_input = {'E' : 'Expert', 'C': 'Causal'}
skill = None

while skill not in valid_input:
    skill=input("What is your skill leavel ? E or C ? (Expert or casual): ")

print(valid_input[skill])

答案 1 :(得分:0)

你可以尝试这样做(注意我对python语法不好):

所以创建一个名为loop的函数,然后添加一个声明if(skill =!c和skill =!e)然后调用自身的语句

答案 2 :(得分:0)

  while True:
    skill=input("What is your skill leavel ? E or C ? (Expert of casual): ")
    if skill == "E":
        print ("Expert")
    elif skill == "C":
        print ("Casual")
    elif skill == "Q":
        break;

在true时使用无限循环,然后在想要退出循环时中断

答案 3 :(得分:0)

以下是您可能需要的代码:

while (True):
    kill=input("What is your skill leavel ? E or C ? (Expert of casual): ")
    if kill == "E":
        print ("Expert")
        break
    if kill == "C":
        print ("Casual")
        break