具有多个变量的Python输入验证...简化了吗?

时间:2018-10-17 02:23:49

标签: python validation input stress

首先,我已经搜索了很多这个站点,并找到了有关该主题的其他帖子,甚至是我正在从事的相同作业,因此代码非常相似。但是,有些事情略有不同。不同。我正在使用“从Python入门,第4版”上这门课,而我的作业来自第5章“ 15.测试平均值和等级”。我已经为除输入验证之外的所有内容编写了代码,我的讲师坚称我们会使用该代码,尽管我们仅应使用本书已涵盖的编码技术:到目前为止,没有列表,元组,字典,lambda等。 ,这使得我在网上(和在本网站上)发现的大多数用于输入验证的示例都没有用,因为我无法弄清楚它们,如果愿意的话也无法使用它们。我已经联系指导老师寻求帮助(在线课程),但是已经有数周没有收到任何回复,所以我在这里。该程序应要求用户输入5个测试分数,找到分数的平均值,为每个分数指定字母等级,然后显示带有字母等级的分数以及平均值。我以为如果使用“ for ranges in range(1,6)”循环询问分数,那么验证输入会更容易,但是我不知道如何访问用户输入的每个分数以发送到define_grade函数,然后再显示在main中(我在下面没有包含任何代码)...因此,我最终为每个分数制作了一个变量,但随后遇到了如何验证输入的问题(确保输入的分数不小于0或大于100,或者确保用户输入了每个变量的数字而不是字母。我希望能够在代码中编写一些异常处理,这样,如果用户输入字母而不是数字,则不会引发异常,因为我的讲师说:“这是我从现在开始尝试的工作。导致您的程序崩溃”,尽管他还没有确切地告诉我如何实现这种输入验证。任何帮助都将不胜感激,我已经为此苦苦挣扎了好几天,这正使我感到非常压力。

编辑:TypeError:input_validation()缺少4个必需的位置参数:'score2','score3','score4'和'score5'是我得到的错误,我知道我做错了,但是,我不知道是什么...我觉得有一个更简单的方法来处理多个变量的输入验证。.由于我仍然很陌生,所以我不知道如何实现它。< / p>

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

1 个答案:

答案 0 :(得分:0)

您的直接错误是您已定义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.')

然后您可以这样称呼它:

score1 = input_validation('Enter the score for the first test: ')
score2 = input_validation('Enter the score for the second test: ')

如果您想将分数保留在列表中,而不要使用五个单独的变量:

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