這是迄今爲止我所擁有的。需要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'
請仔細閱讀錯誤信息,它非常清楚地解釋原因。 – ForceBru
你不應該單獨調用'circle'來傳遞參數並獲取返回值。這些應該發生在同一個電話。 – user2357112
你打給'circle'兩次。仔細看看這兩個電話。 –