Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/353.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输入验证…简化?_Python_Validation_Input_Stress - Fatal编程技术网

多变量Python输入验证…简化?

多变量Python输入验证…简化?,python,validation,input,stress,Python,Validation,Input,Stress,首先,我在这个网站上搜索了很多,找到了关于这个主题的其他帖子,甚至是我正在做的同一个作业,所以代码非常相似。。。然而,有几件事略有不同。我正在学习这门课程,使用《从Python开始》第四版,我的作业来自第5章,第15章。测试平均分和等级。除了输入验证之外,我已经编写了所有的代码,我的导师坚持我们使用输入验证,尽管我们只应该使用书中提到的编码技术:没有列表、元组、字典、lambda等等。。。这使得我在网上和本网站上找到的大多数用于输入验证的示例都毫无用处,因为我无法理解它们,如果我愿意,也无法使用

首先,我在这个网站上搜索了很多,找到了关于这个主题的其他帖子,甚至是我正在做的同一个作业,所以代码非常相似。。。然而,有几件事略有不同。我正在学习这门课程,使用《从Python开始》第四版,我的作业来自第5章,第15章。测试平均分和等级。除了输入验证之外,我已经编写了所有的代码,我的导师坚持我们使用输入验证,尽管我们只应该使用书中提到的编码技术:没有列表、元组、字典、lambda等等。。。这使得我在网上和本网站上找到的大多数用于输入验证的示例都毫无用处,因为我无法理解它们,如果我愿意,也无法使用它们。我已经向在线课程的讲师寻求帮助,但已经好几个星期没有收到任何回复了,所以我在这里。该程序要求用户输入5个测试分数,找到分数的平均值,为每个分数分配一个字母等级,然后显示分数和字母等级以及平均值。我认为如果我要求分数在1,6范围内循环,那么验证输入会更容易,但是我不知道如何访问用户输入的每个分数,以发送到determine_grade函数,然后在main中显示,我没有包含下面的任何代码。。。因此,我最终为每个分数创建了一个变量,但随后我遇到了如何验证输入的问题—确保输入的分数不小于0或大于100,或者用户为每个变量输入了一个数字而不是一个字母。我希望能够在代码中写入一些异常处理,这样,如果用户输入了字母而不是数字,就不会抛出异常,因为我的导师说,从现在起,我的工作就是尝试使您的程序崩溃,尽管他还没有给我回过如何实现这种输入验证的确切信息。任何帮助都将不胜感激,我已经为此挣扎了好几天,这让我非常紧张

编辑:TypeError:input\u验证缺少4个必需的位置参数:“score2”、“score3”、“score4”和“score5”是我遇到的错误,我知道我做错了什么,但是,我不知道是什么。。。我觉得有一种更简单的方法来处理多变量的输入验证。。因为我在这方面还是新手,所以我不知道如何实现它

def get_scores():

score1 = input_validation(float(input('Enter the score for the first test: ')))
score2 = input_validation(float(input('Enter the score for the second test: ')))
score3 = input_validation(float(input('Enter the score for the third test: ')))
score4 = input_validation(float(input('Enter the score for the fourth test: ')))
score5 = input_validation(float(input('Enter the score for the fifth test: ')))
return score1, score2, score3, score4, score5

def input_validation(score1, score2, score3, score4, score5):
while (score1, score2, score3, score4, score5) < 0 or (score1, score2, score3, score4, score5) > 100:
    print('Score cannot be less than 0 or greater than 100!')
    (score1, score2, score3, score4, score5) = float(input('Please enter a correct test score: '))
    return score1, score2, score3, score4, score5

直接的错误是,您定义了input_validation以接受五个参数,但在调用它时只传递了一个参数

在一个函数中收集输入并在另一个函数中验证输入是很尴尬的,因为这些函数必须非常紧密地协调,以允许对错误的输入进行重新编程

同时要求几个分数,然后同时验证所有分数也很尴尬,因为如果一些分数是有效的,而另一些是无效的,你该怎么办?你必须再次要求所有分数,这浪费了用户的时间,或者你需要一种只为无效分数重新计算的方法,这是不必要的复杂性

一次只处理一个分数,将所有输入和验证放在一个地方可能是更好的设计:

def input_validation(prompt):
    # keep looping until they enter a good score
    while True:
        # get the answer as a string
        answer = input(prompt)
        try:
            # convert to float
            score = float(answer)
            # if in range, we're done!  return the converted score number
            if 0 <= score <= 100:
                return score
            # otherwise out of range, print an error message and keep looping
            else:
                print('Score cannot be less than 0 or greater than 100!')
        # if the answer wasn't a number, print an error message and keep looping
        except ValueError:
            print ('That is not a test score.')
如果希望将分数保存在列表中,而不是有五个单独的变量:

scores = []
for i in range(5):
    scores.append(input_validation('Enter the score for test number %d: ' % i+1))

你能重新写一篇文章来更清楚地描述这个问题吗?问题是我需要对五个变量执行相同的输入验证,但不知道如何对每个变量进行验证@约翰·戈登的回答/逻辑是正确的,完全解决了我的问题。正如我之前所说,我对这一点仍然非常、非常陌生,但我渴望尽我所能学习。嘿,谢谢你这么快回复!哇!非常感谢你的帮助!!作为一个令人痛苦的新手,我没有意识到我可以列出像answer=inputprompt或score=floatanswer这样的东西!我的课程中没有涉及这些内容,但是代码非常有效,我想我已经足够理解了,可以在以后的代码中使用类似的东西。再次感谢你!在这个问题上我已经被困了好几天了。
scores = []
for i in range(5):
    scores.append(input_validation('Enter the score for test number %d: ' % i+1))