Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python While循环输入信息后不重复_Python - Fatal编程技术网

Python While循环输入信息后不重复

Python While循环输入信息后不重复,python,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

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

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

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? ")

如果您想重复一个
输入
,直到得到有效答案,然后再问下一个问题,您需要将第一个输入放在
内,而
循环将第二个输入放在循环外,如下所示:

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? ")

如果您想重复一个
输入
,直到得到有效答案,然后再问下一个问题,您需要将第一个输入放在
内,而
循环将第二个输入放在循环外,如下所示:

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? ")

你的while中有一个中断如果星号在星号中,这会中断while循环,移除中断,你也不应该抛出这样的错误,但你会在继续的过程中学习,继续我的朋友。你的代码缩进错误。为了在这里提问,我们需要精确地看到您的缩进。您可以将代码复制/粘贴到框中,然后选择它并按ctrl-K使其统一缩进以进行标记代码格式设置。如果星号在星号中,则会中断while循环,请删除中断,您也不应该抛出类似的错误,但您会随着操作而学习,我的朋友,你的代码缩进不正确。为了在这里提问,我们需要精确地看到您的缩进。您可以将代码复制/粘贴到框中,然后选择它,然后按ctrl-K使其统一缩进,以设置标记代码格式。