Python +;的操作数类型不受支持:';非类型';和';int';
我目前的一段代码就是这样做的:“打印从零到给定最大值的所有完美正方形。此版本经过重构,使其更易于理解和维护。” 目前我的问题与这段代码有关:Python +;的操作数类型不受支持:';非类型';和';int';,python,python-3.x,types,Python,Python 3.x,Types,我目前的一段代码就是这样做的:“打印从零到给定最大值的所有完美正方形。此版本经过重构,使其更易于理解和维护。” 目前我的问题与这段代码有关: def read_bound(prompt): """Reads the upper bound from the standard input (keyboard). If the user enters something that is not a positive integer the function issues
def read_bound(prompt):
"""Reads the upper bound from the standard input (keyboard).
If the user enters something that is not a positive integer
the function issues an error message and retries
repeatedly"""
upper_bound = None
while upper_bound is None:
line = input(prompt)
if line.isnumeric():
return int(line)
else:
print("You must enter a positive number.")
调用主函数时:
def main():
"""Bring everything together"""
lower_bound = read_bound("Enter the lower bound: ")
upper_bound = read_bound("Enter the upper bound: ")
squares = []
for num in range(lower_bound, upper_bound + 1):
if is_perfect_square(num):
squares.append(num)
print_squares(lower_bound, upper_bound, squares)
我得到一个错误:
builtins.TypeError:不支持+:“NoneType”和“int”的操作数类型
为什么用户给出的第一个对象的类型为“none”,而第二个对象的类型为“int”。我希望它们都是整数。我做错了什么
给出的两个答案完全相同,解决了我的问题。因此,我在问题中的守则已被修订。谢谢 函数
read\u-bound
不包含返回语句。如果在Python中执行到函数的末尾,因此上限
将是None
当前,您分配给一个局部变量,该变量与main
中的上限
同时共享名称,这将无效,因为您从未从中读取
修改read\u bound
以包含退货:
def read_bound(prompt):
"""Reads the upper bound from the standard input (keyboard).
If the user enters something that is not a positive integer
the function issues an error message and retries
repeatedly"""
upper_bound = None
while upper_bound is None:
line = input(prompt)
if line.isnumeric():
return int(line) # <-- added
else:
print("You must enter a positive number.")
def read_-bound(提示):
“”“从标准输入(键盘)读取上限。
如果用户输入的不是正整数
函数发出错误消息并重试
反复地
上限=无
虽然上限为“无”:
行=输入(提示)
if line.isnumeric():
return int(line)#函数read_-bound
不包含return语句。如果在Python中执行到函数的末尾,因此上限
将是None
当前,您分配给一个局部变量,该变量与main
中的上限
同时共享名称,这将无效,因为您从未从中读取
修改read\u bound
以包含退货:
def read_bound(prompt):
"""Reads the upper bound from the standard input (keyboard).
If the user enters something that is not a positive integer
the function issues an error message and retries
repeatedly"""
upper_bound = None
while upper_bound is None:
line = input(prompt)
if line.isnumeric():
return int(line) # <-- added
else:
print("You must enter a positive number.")
def read_-bound(提示):
“”“从标准输入(键盘)读取上限。
如果用户输入的不是正整数
函数发出错误消息并重试
反复地
上限=无
虽然上限为“无”:
行=输入(提示)
如果line.isnumeric():
return int(line)#读绑定代码没有返回输入
def read_bound(prompt):
"""Reads the upper bound from the standard input (keyboard).
If the user enters something that is not a positive integer
the function issues an error message and retries
repeatedly"""
upper_bound = None
while upper_bound is None:
line = input(prompt)
if line.isnumeric():
return int(line)
else:
print("You must enter a positive number.")
读取绑定代码未返回输入
def read_bound(prompt):
"""Reads the upper bound from the standard input (keyboard).
If the user enters something that is not a positive integer
the function issues an error message and retries
repeatedly"""
upper_bound = None
while upper_bound is None:
line = input(prompt)
if line.isnumeric():
return int(line)
else:
print("You must enter a positive number.")
您的问题缩进是否适用于read\u bound
?如果没有,请修复read\u-bound()
不会返回任何内容您的问题缩进是否正确?如果没有,请修复read\u-bound()
没有返回任何内容