【问题标题】:How can I add an input loop?如何添加输入循环?
【发布时间】:2015-09-22 17:28:01
【问题描述】:
classes = input("There are three types of classes in this game. Warrior, Wizard and Hunter. Pick one.")

if classes == "Warrior" or "Wizard" or "Hunter":
    print ("Good choice.. the "+classes+" will dominate your opponents!")
else:
    print ("That's not a class! Pick again!")

#基本上,我想添加一个循环,以便它会再次提问。

请记住,我使用的是 Python-3

【问题讨论】:

标签: python loops input


【解决方案1】:
classes = input("There are three types of classes in this game. Warrior, Wizard and Hunter. Pick one.\n")

while classes not in ("Warrior", "Wizard", "Hunter"):
  print ("That's not a class! Pick again!\n")
  classes = input("There are three types of classes in this game. Warrior, Wizard and Hunter. Pick one.\n")

print ("Good choice.. the "+classes+" will dominate your opponents!\n")

【讨论】:

    【解决方案2】:
    characters = ("Warrior", "Wizard", "Hunter")
    
    while True:
      classes = input("There are three types of classes in this game. Warrior, Wizard and Hunter. Pick one.")
      if classes in characters:
        break
      else:
        print ("That's not a class! Pick again!")
    
    print ("Good choice.. the "+classes+" will dominate your opponents!")
    

    【讨论】:

    • 这是原始问题的引述:)
    猜你喜欢
    • 1970-01-01
    • 2023-03-13
    • 2022-01-05
    • 1970-01-01
    • 2014-01-03
    • 1970-01-01
    • 2022-11-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多