Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python +;的操作数类型不受支持:';非类型';和';int';_Python_Python 3.x_Types - Fatal编程技术网

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()
没有返回任何内容