你如何解决这个问题,而循环?

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


代码是这样的:


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:

不起作用的部分是这样的:


else:

    while choice > 6 or choice < 1:

        print("Please choose one of the 6 groups")

        choice = int(input())

循环继续进行,并且没有我输入的内容(除非我输入除整数以外的任何内容,否则它将显示错误消息)。当我输入1到6之间的整数时,我如何停止此操作?


www说
浏览 137回答 2
2回答

杨魅力

如果<6,你可能永远不会从循环中走出来,所以你的程序永远不会碰到你的循环。whilechoicefor修复你可以做一个保存所有选项,阅读一次和基于输入创建dictinputcontestantsoptions = {&nbsp; &nbsp; 1: ["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"],&nbsp; &nbsp; 2: ["Nina Flowers", "Raven", "Manila Luzon", "Chad Micheals", "Alaska", "Courtney Act", "Ginger Minj", "Kim Chi", "Peppermint", "Eureka", "Brooke Lynn Hytes", "Katya", "Kennedy Davenport", "Monique Heart"],&nbsp; &nbsp; 3: ["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"],&nbsp; &nbsp; 4: ["Nina Flowers", "Pandora Boxx", "Yara Sofia", "Latrice Royale", "Ivy Winters", "BenDeLaCreme", "Katya", "Cynthia Lee Fontaine", "Valentina", "Monét X Change", "Nina West"],&nbsp; &nbsp; 5: ["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"],&nbsp; &nbsp; 6: ["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"]}print('''Which group of queens do you want to compete against?1. Winners2. Runner-ups3. Lip sync assassins4. Miss Congenialities5. First Eliminated6. Returning queens''')choice = int(input())while choice > 6 or choice < 1:&nbsp; &nbsp; print("Please choose one of the 6 groups")&nbsp; &nbsp; choice = int(input())contestants = options[choice]

温温酱

正如我所看到的,你添加了更多的代码。您正在检查您的选择是否大于&nbsp;6,然后与小于 6 的值进行比较。你从来没有碰到过那个循环。while无需遍历索引。直接遍历列表。for&nbsp;x&nbsp;in&nbsp;contestants: &nbsp;&nbsp;&nbsp;&nbsp;print(x) &nbsp;&nbsp;&nbsp;&nbsp;time.sleep(0.5)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python