2017-06-16 76 views
-2

这是迄今为止我所拥有的。需要0个位置参数,但1个被赋予或缺少1个需要的位置参数:'半径'取决于我如何编写代码

# This program uses a value returning function named circle 
# that takes the radius of a circle and returns the area and the 
# circumference of the circle. 

##################################### 
# Start program 
# Main() 
# Get user input of radius from user. 
# Pass argument radius to circle function 
# circle() 
# Calculate circumference 
# Calculate area 
# Return values from circle to main 
# Main() 
# Print circumference 
# Print area 
# End program 
##################################### 

# First we must import the math functions 
import math 

# Start of program 
def main(): 
    # Get circle's radius from user 
    radius=float(input('Enter the radius of the circle: ')) 

    #Calling the circle funcion while passing radius to it 
    circle(radius) 

    #Gathering returned results from circle function 
    circumference, area = circle() 

    #Printing results 
    print('Circle circumference is ', circumference) 
    print('Circle area is ', area) 

# Circle function 
def circle(radius): 

    # Returning results to main 
    return 2 * math.pi * radius, math.pi * radius**2 

# End program 
main() 

但我得到这个错误:

Enter the radius of the circle: 2 Traceback (most recent call last):
File "/Users/shawnclark/Documents/ Introduction to Computer Programming/Chapter 05/Assignment/5.1.py", line 45, in main() File "/Users/shawnclark/Documents/ Introduction to Computer Programming/Chapter 05/Assignment/5.1.py", line 32, in main circumference, area = circle() TypeError: circle() missing 1 required positional argument: 'radius'

+0

请仔细阅读错误信息,它非常清楚地解释原因。 – ForceBru

+0

你不应该单独调用'circle'来传递参数并获取返回值。这些应该发生在同一个电话。 – user2357112

+0

你打给'circle'两次。仔细看看这两个电话。 –

回答

1

有两个问题与您的代码。

问题1:语法

由于您的错误信息中明确指出你所呼叫的圆圈()函数,不传递任何参数;但是您将circle()函数定义为接受一个参数。

问题2:误区返回值是如何工作的

你叫圈传递半径,但是你忽略了返回值。稍后尝试利用circle()的返回值而不传递半径。您应该删除第一个调用circle()并修改第二个调用以包含radius参数。

circumference, area = circle(radius)

0

了那里:

Start of program def main():

# Get circle's radius from user 
radius=int(input('Enter the radius of the circle: ')) 
# Catching 
circumference, area = circle(radius) 
# Print result 
print('Circle circumference is ',circumference) 
print('Circle area is ',area) 

Circle function def circle(rad):

# Calculate circumference 
circumference = rad * 2 * math.pi 
# Calculate area 
area=rad ** 2 * math.pi 
# Return values 
return circumference, area 

End program main()

感谢您帮助一个noob。

相关问题