while循环不会重复输入

时间:2016-08-26 00:36:43

标签: python

制作一个包含不同星号的列表的程序,然后要求用户输入它们是什么星号,然后让程序在继续之前检查列表中包含的星号。

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

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 个答案:

答案 0 :(得分: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? ")