2017-06-16 72 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。

相關問題