Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/313.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
TypeError:sphereArea()缺少1个必需的位置参数:';半径';使用python_Python - Fatal编程技术网

TypeError:sphereArea()缺少1个必需的位置参数:';半径';使用python

TypeError:sphereArea()缺少1个必需的位置参数:';半径';使用python,python,Python,我正在尝试创建一个代码,该代码将返回球体的表面积,但我不断得到错误sphereArea()缺少1个必需的位置参数:“radius” import math def sphereArea(radius): radius= int(input("The number you want as radius:")) surface_area= 4*math.pi*radius**2 print("The surface area of the sphere is = "

我正在尝试创建一个代码,该代码将返回球体的表面积,但我不断得到错误sphereArea()缺少1个必需的位置参数:“radius”

 import math
 def sphereArea(radius):
     radius= int(input("The number you want as radius:"))
     surface_area= 4*math.pi*radius**2
     print("The surface area of the sphere is = ", surface_area)
 sphereArea()

如果要求用户在函数
sphereArea
中输入半径,则不需要将其作为参数:

 import math

 def sphereArea():
     radius = int(input("The number you want as radius:"))
     surface_area = 4 * math.pi * radius**2
     print("The surface area of the sphere is = ", surface_area)

 sphereArea()

您只是没有将参数传递给函数调用“sphereArea”以适应参数半径。此外,在函数定义的第一个lint中重新指定该值,这将不起作用。你试图同时做两件事,一个功能做得太多了。通常,您希望函数返回一个值,然后使用该值

 import math
 def sphereArea(radius):
     radius= int(input("The number you want as radius:")) # Here is bad
     surface_area= 4*math.pi*radius**2
     print("The surface area of the sphere is = ", surface_area)
 sphereArea() # Here as well
新代码:

import math
 def sphereArea(radius):
     surface_area = 4*math.pi*radius**2
     return surface_area

inp_radius = int(input("The number you want as radius:"))
surface_area = sphereArea(inp_radius)
print("The surface area of the sphere is = ", surface_area)

定义函数时,括号中的部分是列出要传递到函数中的任何参数的地方。你可以把参数想象成输入。如果用输入定义函数,但不给函数任何输入,python将给您一个错误。有两种可能修复此代码的方法

方法1(此处函数不需要任何输入/参数):

方法2(我们移动输入行,因为它在函数中变得多余。其思想是将值作为输入传递到函数中):


这就是问题的实质;编写此函数的定义;sphereArea(radius)返回具有给定半径的球体的面积。抱歉,这是一个麻烦,但是在使用代码并在最后键入sphereArea()后,它仍然会给出相同的错误。。我不应该使用它,或者重申一下:您对函数sphereArea的定义中包含一个参数“radius”。这意味着,当调用函数时,它需要传递一个参数来代替radius。因此,当您调用sphereArea时,需要在两个参数之间传递一个数字。i、 e.sphereArea(10)或sphereArea(某些变量)。其他函数有多个参数,如add(6,10)或add(10,一些数字)。无论如何,在我写的代码中,你的函数已经被调用并传递了一个变量,这个变量是你得到的用户输入。
 import math
 def sphereArea():     #Just get rid of the argument in your function definition
     radius= int(input("The number you want as radius:"))
     surface_area= 4*math.pi*radius**2
     print("The surface area of the sphere is = ", surface_area)
 sphereArea()
 import math
 def sphereArea(radius):
     radius= int(input("The number you want as radius:"))
     surface_area= 4*math.pi*radius**2
     print("The surface area of the sphere is = ", surface_area)

 required_radius= int(input("The number you want as radius:")) 
 sphereArea(required_radius) #You have now passed your value into the function to be used.