我在里面有另一个功能的功能。它给我类型错误(Python)
I have function with another function inside. It gives me Type Error (Python)
def cylinder():
r = int(input("Radius = "))
h = int(input("Height = "))
s = 2 * 3.14 * r * h
if input("Do you want to know full area? [y/n]: ") == "y":
s += 2 * circle(r)
print(s)
def circle(r):
s1 = 3.14*r*r
cylinder()
这是我的代码,我有错误:
File "C:\Users\Good dogie\Desktop\python-work\main.py", line 187, in <module>
cylinder()
File "C:\Users\Good dogie\Desktop\python-work\main.py", line 182, in cylinder
s += 2 * circle(r)
TypeError: unsupported operand type(s) for *: 'int' and 'NoneType'
我明白错误的含义,但我不知道如何解决这个问题。如果有人能给我小费,我将不胜感激。
您没有return从 circle() 中获取值。
当 Circle 运行它时 returns None。
在代码中添加 return 将停止此错误。
def cylinder():
r = int(input("Radius = "))
h = int(input("Height = "))
s = 2 * 3.14 * r * h
if input("Do you want to know full area? [y/n]: ") == "y":
s += 2 * circle(r)
print(s)
def circle(r):
s1 = 3.14*r*r
return s1
cylinder()
您收到此错误是因为您没有在 circle
中指定 return 值,而 Python 中函数的默认 return 值是 None
。您需要在 circle
函数中添加 return 语句,如下所示:
def cylinder():
r = int(input("Radius = "))
h = int(input("Height = "))
s = 2 * 3.14 * r * h
if input("Do you want to know full area? [y/n]: ") == "y":
s += 2 * circle(r)
print(s)
def circle(r):
s1 = 3.14 * r * r
return s1
cylinder()
但是,您也可以直接 return 值而无需为其创建变量。此外,您应该使用 math.pi
而不是 3.14
:
import math
def cylinder():
r = int(input("Radius = "))
h = int(input("Height = "))
s = 2 * math.pi * r * h
if input("Do you want to know full area? [y/n]: ") == "y":
s += 2 * circle(r)
print(s)
def circle(r):
return math.pi * r * r
cylinder()
def cylinder():
r = int(input("Radius = "))
h = int(input("Height = "))
s = 2 * 3.14 * r * h
if input("Do you want to know full area? [y/n]: ") == "y":
s += 2 * circle(r)
print(s)
def circle(r):
s1 = 3.14*r*r
cylinder()
这是我的代码,我有错误:
File "C:\Users\Good dogie\Desktop\python-work\main.py", line 187, in <module>
cylinder()
File "C:\Users\Good dogie\Desktop\python-work\main.py", line 182, in cylinder
s += 2 * circle(r)
TypeError: unsupported operand type(s) for *: 'int' and 'NoneType'
我明白错误的含义,但我不知道如何解决这个问题。如果有人能给我小费,我将不胜感激。
您没有return从 circle() 中获取值。 当 Circle 运行它时 returns None。 在代码中添加 return 将停止此错误。
def cylinder():
r = int(input("Radius = "))
h = int(input("Height = "))
s = 2 * 3.14 * r * h
if input("Do you want to know full area? [y/n]: ") == "y":
s += 2 * circle(r)
print(s)
def circle(r):
s1 = 3.14*r*r
return s1
cylinder()
您收到此错误是因为您没有在 circle
中指定 return 值,而 Python 中函数的默认 return 值是 None
。您需要在 circle
函数中添加 return 语句,如下所示:
def cylinder():
r = int(input("Radius = "))
h = int(input("Height = "))
s = 2 * 3.14 * r * h
if input("Do you want to know full area? [y/n]: ") == "y":
s += 2 * circle(r)
print(s)
def circle(r):
s1 = 3.14 * r * r
return s1
cylinder()
但是,您也可以直接 return 值而无需为其创建变量。此外,您应该使用 math.pi
而不是 3.14
:
import math
def cylinder():
r = int(input("Radius = "))
h = int(input("Height = "))
s = 2 * math.pi * r * h
if input("Do you want to know full area? [y/n]: ") == "y":
s += 2 * circle(r)
print(s)
def circle(r):
return math.pi * r * r
cylinder()