繁体   English   中英

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

[英]Python Input Validation with multiple variables…simplified?

首先,我搜索了很多该站点,并找到了有关该主题的其他帖子,甚至是我正在从事的相同任务,因此代码非常相似...但是,有些地方略有不同。 我正在使用“从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'是我得到的错误,我知道我做错了,但是,我不知道不知道是什么...我觉得有一个更简单的方法来处理多个变量的输入验证。.由于我还很陌生,所以我不知道如何实现它。

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.')

然后您可以这样称呼它:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM