Beginning Python - Calculating Area of a Circle
首先,我使用的是pyscripter ver。2.6.0.0 x64,带python版本。3.3.0 x64
我正在上一门关于Python的入门课程,我完全被难住了。我被问到这个问题:
编写一个名为calculateArea的函数。函数应采用表示圆半径的参数,并返回圆的面积。编写一个程序,要求用户输入圈数。然后,程序应该显示一个图表,列出圆的半径和它的面积,从1开始,一直到并包括用户输入的数字。
这是我迄今为止取得的进展:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | 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圈进行测试时的电流输出:
1 2 3 4 5 6 7 8 9 | >>> Circle('s) Radius Given Calculated Area ********** ************ *************** 1 78.525 2 78.525 3 78.525 4 78.525 5 78.525 >>> |
我现在想弄明白的是,如何修正输出,它从我输入的n中计算出被测圆的面积。然后我需要帮助来获得半径来显示/工作。当我使用r让一个用户输入一个起始半径时,我一直得到错误:
1 2 3 4 | 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 2 3 4 5 6 7 8 9 10 11 12 13 | 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)) ) |
输出:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | Please enter the amount of circles you wish to test: 15 Circle('s) Radius Given Calculated Area ---------- --------------- --------------- 1 1 3.14159265359 2 2 12.5663706144 3 3 28.2743338823 4 4 50.2654824574 5 5 78.5398163397 6 6 113.097335529 7 7 153.938040026 8 8 201.06192983 9 9 254.469004941 10 10 314.159265359 11 11 380.132711084 12 12 452.389342117 13 13 530.929158457 14 14 615.752160104 15 15 706.858347058 |