2016-08-26 32 views
1

制作一个包含不同星号的列表的程序,然后要求用户输入他们的星号,然后让程序在移动之前检查它是否包含在列表中上。while循环不会重复一次输入信息

问题是它确实检查它是否在列表中,但它不会重复。

play = True 

while play: 
    print("Welcome to my Pseudo_Sammy program, please enter your name, star sign and then your question by typing it in and pressing the enter key, and I will give you the answer to your question") 
    name = input("What do they call you? ") 
    starsigns = ("leo", "virgo", "libra", "scorpio", "sagittarius", "capricorn", "aquarius", "pisces", "aries", "taurus", "gemini", "cancer") 
    starsign = str(input("What star do you come from? ")).lower() 
    while True: 
     try:    
      if starsign in starsigns: 
       break 
      else: 
       raise 
     except: 
      print("Please enter a valid star sign") 
      question = input("What bothers you dear? ") 
+1

如果星标是在starsigns中,你有一个休息时间,这打破了while循环,删除休息,你也不应该抛出这样的错误,但你会学习,当你走,继续我的朋友。 –

+0

您的代码被错误地缩进。为了在这里提出问题,我们需要确切地看到你的缩进。您可以将代码复制/粘贴到[编辑]框中,然后将其选中,然后按Ctrl-K使其统一缩进以进行Markdown代码格式化。 – tripleee

回答

0

,如果你想,直到你获得一个有效的答案,然后问下一个问题要重复的input,你需要把里面while循环的第一个输入和环外的第二个输入,就像这样:

starsigns = ("leo", "virgo", ...) 
starsign = None 
while starsign not in starsigns: 
    if starsign: 
     print("Please enter a valid star sign: {}.".format(", ".join(starsigns))) 
    starsign = input("What start do you come from? ").lower().strip() 
question = input("What bothers you dear? ")