接受某些答案

时间:2017-10-07 14:38:03

标签: python variables conditional-statements

我在python中编写一个脚本,要求用户进入他们的Hogwarts房子。然后将其保存到变量中。如何创建条件检查以确保它们已进入有效房屋?

1 个答案:

答案 0 :(得分:1)

说你有代码:

houses = ["Slytherin", "Gryffindor", "Hufflepuff", "Ravenclaw"]
inp = raw_input("What house are you in? ")

if inp in houses:
    print("Valid house!")
else:
    print("Invalid house!")

示例输出:

  你住在哪个房子?斯莱特林
  有效的房子!

     你住在哪个房子?一个
  房子无效!

要循环运行:

houses = ["Slytherin", "Gryffindor", "Hufflepuff", "Ravenclaw"]
valid = False

while not valid:
    inp = raw_input("What house are you in? ")
    if inp in houses:
        print("Valid house!")
        valid = True
    else:
        print("Invalid house!")