从Python开始 - 计算圆的面积

时间:2017-04-29 21:39:47

标签: python

对于初学者我正在使用Pyscripter ver。 2.6.0.0 x64 with Python ver。 3.3.0 x64

我正在开始一个python课程而且我完全难过了。我得到了这个问题:

编写一个名为calculateArea的函数。该函数应采用表示圆的半径的参数,并应返回圆的面积。编写一个程序,要求用户输入圆圈数。然后,程序应显示一个图表,列出圆的半径,它的区域从1开始,一直到并包括用户输入的数字。

这是我迄今取得的进步:

def calculateArea(radius):
    pi = 3.141
    area = (pi * (radius ** 2))
    return area

n = int(input("Please enter the amount of circles you wish to test."))

#r = float(input("Please enter the circle's radius."))

area = calculateArea(n)

#print("The area of the circle with a radius of:", r, "equals:", area)

print("Circle('s)","  ","Radius Given","  ", "Calculated Area")
print("**********", "  ", "************", "  ", "***************")

for x in range(1, n+1):
    print(x, "     ","     ", "    ","          ", area)

当我输入5个圆圈进行测试时,这是我当前的输出:

>>> 
Circle('s)    Radius Given    Calculated Area
**********    ************    ***************
1                             78.525
2                             78.525
3                             78.525
4                             78.525
5                             78.525
>>> 

我现在要弄清楚的是如何修复输出,它从我输入的n计算测试圆圈的区域。然后我需要帮助尝试获得显示/工作的半径。当我使用r让用户输入一个开始半径时,我不断收到错误:

Traceback (most recent call last):
  File "<string>", line 420, in run_nodebug
  File "<module1>", line 28, in <module>
TypeError: 'float' object cannot be interpreted as an integer

1 个答案:

答案 0 :(得分:0)

这可能会起到作用:

typedef void (*ExPointer)(int , int);

输出:

import math
def calculateArea(radius):
    area = (math.pi * (radius**2))
    return area

n = int(input("Please enter the amount of circles you wish to test: "))

print("{:10} {:15} {:15}".format("Circle('s)","Radius Given","Calculated Area") )
print("{:10} {:15} {:15}".format("-"*10,"-"*15,"-"*15) )


for x in range(1, n + 1):
    print("{:10} {:15} {:15}".format(x, x, calculateArea(x)) )