Python TypeError:int() 参数必须是字符串、类似字节的对象或数字,而不是“NoneType”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/47253696/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'
提问by Ben Grzybowski
I am a student and am pretty new to python. I am trying to validate the user input to check whether or not they entered a 3 digit number, they should only be able to enter a 2 digit number. The program works fine when the user enters only 2 digits but when they enter 3 or more, it throws the error message I created and asks them to re-enter a number that is only 2 digits, then once they re-enter a 2 digit number python gives me the TypeError. The full error I am getting is:
我是一名学生,对 python 很陌生。我正在尝试验证用户输入以检查他们是否输入了 3 位数字,他们应该只能输入 2 位数字。当用户只输入 2 位数字时,程序运行良好,但当他们输入 3 位或更多位时,它会抛出我创建的错误消息并要求他们重新输入一个只有 2 位数字的数字,然后一旦他们重新输入 2 位数字number python 给了我 TypeError。我得到的完整错误是:
Traceback (most recent call last):
File "/Users/admin/Desktop/week6/question_7.py", line 87, in <module>
File "/Users/admin/Desktop/week6/question_7.py", line 79, in do_stuff
print (display_grade(user_in))
TypeError: int() argument must be a string, a bytes-like object or a
number, not 'NoneType'
I am having trouble figuring out why it is doing this. I have a feeling it has something to do with the calling the get_input() function inside of itself? I can't figure out how to avoid this though.
我很难弄清楚为什么要这样做。我有一种感觉,它与调用自身内部的 get_input() 函数有关?我不知道如何避免这种情况。
I searched the error I keep getting and couldn't find anything that helped.
我搜索了我不断收到的错误,但找不到任何有用的东西。
Here's the program, it's pretty simple.
这是程序,它非常简单。
NUMBER_OF_TESTS = 5
def get_input ():
print ('enter a test score below')
user_in = input()
if len(user_in) > 2:
print ('error, you can only enter a 2 digit number.')
get_input()
else:
return user_in
def display_grade (letter_grade):
if letter_grade >= 89:
return ('You got an A!')
elif letter_grade >= 80 and letter_grade <= 89:
return ('You got a B!')
elif letter_grade >= 70 and letter_grade <= 79:
return ('You got a C')
elif letter_grade >= 61 and letter_grade <= 69:
return ('You got a D')
else:
return ('You got an F')
def calAverage(total):
average_score = (total) / NUMBER_OF_TESTS
return int(average_score)
def do_stuff():
i = 0
x = 0
while i < NUMBER_OF_TESTS:
user_in = int(get_input())
print (display_grade(user_in))
x = x + user_in
i = i + 1
print ('your average score is' , calAverage(x) , 'which means' , display_grade(calAverage(x)))
do_stuff()
回答by Patrick Haugh
def get_input ():
print ('enter a test score below')
user_in = input()
if len(user_in) > 2:
print ('error, you can only enter a 2 digit number.')
get_input()
else:
return user_in
The problem is the recursive get_input()
call. When you make that call, you're not returning the value you get, so None
is returned instead. Replace it with return get_input()
. Also, your function doesn't allow for people to get 100 on a test. I don't know if that's intentional.
问题是递归get_input()
调用。当您进行该调用时,您并没有返回获得的值,None
而是返回值。将其替换为return get_input()
. 此外,您的功能不允许人们在测试中获得 100。不知道是不是故意的