Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/278.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循环中的列表,而不追加结束while循环的变量_Python_Python 3.x - Fatal编程技术网

Python 尝试将变量追加到while循环中的列表,而不追加结束while循环的变量

Python 尝试将变量追加到while循环中的列表,而不追加结束while循环的变量,python,python-3.x,Python,Python 3.x,我有问题。代码可以工作,但我希望它在不附加用户最终输入的情况下工作。 不确定如何在不将不正确的变量放入末尾列表的情况下结束循环。 测验=[] #Create variable to start the counter in the while loop QuizNumber = 1 #Create variable to start while loop QuizValue = 0 #Create while loop that ends if the user types anything

我有问题。代码可以工作,但我希望它在不附加用户最终输入的情况下工作。 不确定如何在不将不正确的变量放入末尾列表的情况下结束循环。 测验=[]

#Create variable to start the counter in the while loop
QuizNumber = 1

#Create variable to start while loop
QuizValue = 0

#Create while loop that ends if the user types anything outside of 0 and 15
while int(QuizValue) >= 0 and int(QuizValue) <= 15:

    #Get user input for quiz values
    QuizValue = input("Quiz " +str(QuizNumber) + ":")



    #Make if statement to end while loop if user types anything not integer
    if int(QuizValue) >= 0 or int(QuizValue) <= 15:
        QuizValue = QuizValue

        #Append to list
        Quizzes.append(QuizValue)

    else:
        QuizValue = 999


    #Counter for quiz number
    QuizNumber = QuizNumber + 1
#创建变量以启动while循环中的计数器
QuizNumber=1
#创建变量以在循环时启动
QuizValue=0
#创建while循环,如果用户键入0和15之外的任何内容,该循环将结束

而int(QuizValue)>=0和int(QuizValue)=0或int(QuizValue)在循环后尝试
Quizzes=Quizzes[:-1]


这是一个新的列表,除了最后一个项目

之外,一个常见的Python熟语是使用<代码> 1:开始一个“无限”循环,但是在满足某个条件时,在循环的中间有一个<代码>破解< /Cord>语句。这将取代
do。。。直到在其他语言中找到
表单。因此:

while 1:  # an "infinite" loop
    QuizValue = input("Quiz " +str(QuizNumber) + ":")
    # Do something if QuizValue is between 0 and 15
    if 0 <= int(QuizValue) <= 15:
        Quizzes.append(QuizValue)
    else:
        break
while1:#一个“无限”循环
QuizValue=输入(“quick”+str(QuizNumber)+“:”)
#如果QuizValue介于0和15之间,请执行某些操作
如果0弹出
您可以使用
pop
删除最后一项:

QuizNumber = 1
QuizValue = 0
while 0 <= int(QuizValue) <= 15:
    QuizValue = input("Quiz " +str(QuizNumber) + ":")
    Quizzes.append(QuizValue)
    QuizNumber = QuizNumber + 1
Quizzes.pop()

再次注意,
x<0或x>15
not(0在处理来自用户的意外输入时始终使用try,except子句相同。
下面的代码使用布尔标志来中断while循环并避免else条件

QuizNumber = 1
QuizValue = 0
Quizzes = []
flag = True

while flag:
    try:
        QuizValue = input("Quiz " +str(QuizNumber) + ":")
        flag = False
        if str(QuizValue).isdigit():
            if int(QuizValue) >= 0 and int(QuizValue) <= 15:
                Quizzes.append(QuizValue)
                QuizNumber = QuizNumber + 1
                flag = True
    except Exception as e:
        flag = False
QuizNumber=1
QuizValue=0
测验=[]
flag=True
而国旗:
尝试:
QuizValue=输入(“quick”+str(QuizNumber)+“:”)
flag=False
如果str(QuizValue).isdigit():

如果int(QuizValue)>=0和int(QuizValue)这行是什么:QuizValue=QuizValue?我想我不需要它。重点是QuizValue=999来结束循环。我删除了它,但仍然遇到同样的问题。
Quizzes.pop()。谢谢!您知道通过按enter键结束循环的方法吗?现在它给了我“ValueError:invalid literal for int(),以10为基数:”。当您将任意字符串转换为整数时,它可能会引发异常而失败。您将希望捕获该异常。在您的情况下,我将删除
int(quick\u value)
并将您的
input()
调用放入一个
int()
调用,如下所示:
quick\u value=int(input(“quick{n}:”).format(n=quick\u number))
。然后您只需将该行换行,尝试处理异常(请参见此处:)。好的。谢谢。我会尝试的。
QuizNumber = 1
QuizValue = 0
while True:
    QuizValue = input("Quiz " +str(QuizNumber) + ":")
    if not (0 <= int(QuizValue) <= 15):
        break
    Quizzes.append(QuizValue)
    QuizNumber = QuizNumber + 1
    quiz_value = input("Quiz " +str(quiz_number) + ":")
    quiz_value = input("Quiz {n}:".format(n=quiz_number))
quiz_number = 1
quiz_value = 0
while 0 <= int(quiz_value) <= 15:
    quiz_value = input("Quiz {n}:".format(n=quiz_number))
    quizzes.append(quiz_value)
    quiz_number = quiz_number + 1
quizzes.pop()
QuizNumber = 1
QuizValue = 0
Quizzes = []
flag = True

while flag:
    try:
        QuizValue = input("Quiz " +str(QuizNumber) + ":")
        flag = False
        if str(QuizValue).isdigit():
            if int(QuizValue) >= 0 and int(QuizValue) <= 15:
                Quizzes.append(QuizValue)
                QuizNumber = QuizNumber + 1
                flag = True
    except Exception as e:
        flag = False