【问题标题】:Python Input Validation with multiple variables...simplified?具有多个变量的 Python 输入验证...简化了吗?
【发布时间】:2018-10-17 02:23:48
【问题描述】:

首先,我在这个网站上搜索了很多,并找到了关于这个主题的其他帖子,甚至是我正在处理的相同作业,所以代码非常相似......但是,有一些事情稍微不同的。我正在学习这门课程,使用“Python 入门,第 4 版”,我的作业来自第 5 章“15. 测试平均分和成绩”。我已经为除了输入验证之外的所有内容编写了代码,我的导师坚持我们使用它,尽管我们只应该使用本书已经涵盖的编码技术:没有列表、元组、字典、lambda 等。 . 这使得我在网上(和本网站)找到的大多数用于输入验证的示例都毫无用处,因为我无法弄清楚它们,如果我想的话也无法使用它们。我已经向讲师寻求帮助(在线课程),但几周没有收到任何回复,所以我在这里。该程序应该要求用户输入 5 个测试分数,找到分数的平均值,为每个分数分配一个字母等级,然后显示带有字母等级的分数以及平均值。我认为如果我通过“for score in range(1, 6)”循环询问分数,验证输入会更容易,但是我不知道如何访问用户输入的每个分数以发送到determine_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

【问题讨论】:

  • 你能重写一下更清楚地描述问题吗?
  • 问题是我需要对五个变量执行相同的输入验证,但不知道如何为每个变量执行此操作。 @John Gordon 的回答/逻辑是正确的,并且完美地解决了我的问题。正如我之前所说,我对此仍然非常非常陌生,但渴望尽我所能学习。

标签: python validation input stress


【解决方案1】:

您的直接错误是您已将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))

【讨论】:

  • 您好,感谢您这么快回复!哇!十分感谢你的帮助!!作为一个令人痛苦的新手,我不知道我可以列出诸如“answer = input(prompt)”或“score = float(answer)”之类的内容!我的课程中没有涉及它们,但代码运行良好,我认为我理解它足够好,可以在我的代码中使用类似的东西。再次,谢谢你!我在这个问题上被困了好几天。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-28
  • 2023-03-05
  • 2020-04-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多