这是迄今为止我所拥有的。需要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'两次。仔细看看这两个电话。 –