2016-08-26 58 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? ")