这是我到目前为止所做的。
# 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()
但是我收到了这个错误:
输入圆的半径:2 Traceback(最近一次呼叫最后一次):
文件“/ Users / shawnclark / Documents /计算机简介 编程/第05章/作业/ 5.1.py“,第45行,in main()文件“/ Users / shawnclark / Documents /计算机编程简介/第05章/作业/ 5.1.py”,第32行,主要 周长,面积=圆()TypeError:circle()缺少1个必需的位置参数:'radius'
答案 0 :(得分:1)
您的代码存在两个问题。
问题1:语法
由于您的错误消息清楚地表明您正在调用circle()函数而不传递任何参数;但是你将circle()函数定义为一个参数。
问题2:误解返回值的工作原理
您调用圆圈传递半径,但忽略了返回值。稍后您尝试使用circle()的返回值而不传递半径。您应该删除对circle()的第一次调用,并修改第二次调用以包含radius参数。
circumference, area = circle(radius)
答案 1 :(得分:0)
到了那里:
程序开始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)
圆函数def circle(rad):
# Calculate circumference circumference = rad * 2 * math.pi # Calculate area area=rad ** 2 * math.pi # Return values return circumference, area
结束程序main()
感谢您帮助一个菜鸟。