首页 > 解决方案 > 你如何解决这个while循环?

问题描述

编辑:我有另一个问题,但它不会让我再次发布

代码是这样的:

print('''Which group of queens do you want to compete against?
1. Winners
2. Runner-ups
3. Lip sync assassins
4. Miss Congenialities
5. First Eliminated
6. Returning queens''')
choice = int(input())

while choice > 6:
  if choice == 1:
    contestants = ["BeBe Zahara Benet", "Tyra Sanchez", "Raja", "Sharon Needles", "Jinkx Monsoon", "Bianca Del Rio", "Violet Chachki", "Bob the Drag Queen", "Sasha Velour", "Aquaria", "Yvie Oddly", "Chad Michaels", "Alaska", "Trixie Mattel", "Trinity the Tuck", "Monét X Change"]
  elif choice == 2:
    contestants = ["Nina Flowers", "Raven", "Manila Luzon", "Chad Micheals", "Alaska", "Courtney Act", "Ginger Minj", "Kim Chi", "Peppermint", "Eureka", "Brooke Lynn Hytes", "Katya", "Kennedy Davenport", "Monique Heart"]
  elif choice == 3:
    contestants = ["Akashia", "Jujubee", "Alexis Mateo", "Latrice Royale", "Coco Montrese", "Trinity K. Bonet", "Kennedy Davenport", "Chi Chi DeVayne", "Peppermint", "Kameron Michaels", "Ra'Jah O'Hara", "Raven", "Alaska", "BenDeLaCreme", "Trinity the Tuck"]
  elif choice == 4:
    contestants = ["Nina Flowers", "Pandora Boxx", "Yara Sofia", "Latrice Royale", "Ivy Winters", "BenDeLaCreme", "Katya", "Cynthia Lee Fontaine", "Valentina", "Monét X Change", "Nina West"]
  elif choice == 5:
    contestants = ["Victoria Parker", "Shangela", "Venus D-Lite", "Alisa Summers", "Penny Tration", "Kelly Mantle", "Tempest DuJour", "Laila McQueen", "Jaymes Mansfield", "Vanessa Vanjie Mateo", "Soju", "Mimi Imfurst", "Coco Montrese", "Thorgy Thor", "Jasmine Masters"]
  elif choice == 6:
    contestants = ["Carmen Carrera", "Shangela", "Kenya Michaels", "Trixie Mattel", "Naysha Lopez", "Cynthia Lee Fontaine", "Eureka", "Vanessa Vanjie Mateo", "Alyssa Edwards", "Tatianna", "Morgan McMichaels", "Latrice Royale", "Manila Luzon"]
  else:
    while choice > 6 or choice < 1:
        print("Please choose one of the 6 groups")
        choice = int(input())

不起作用的部分是:

else:
    while choice > 6 or choice < 1:
        print("Please choose one of the 6 groups")
        choice = int(input())

循环继续进行,无论我输入什么(除非我输入除整数以外的任何内容,它会显示错误消息)。当我输入 1 到 6 之间的整数时,如何停止这种情况?

标签: pythonloopswhile-loop

解决方案


编辑:正如我所见,您添加了更多代码。您正在检查您的选择是否大于6,然后与小于 6 的值进行比较。您永远不会遇到该while循环。

无需遍历索引。直接循环列表。

for x in contestants:
    print(x)
    time.sleep(0.5)

推荐阅读