【问题标题】:Python Calculating the perimeter of a circlePython计算圆的周长
【发布时间】:2013-05-20 01:22:45
【问题描述】:

我正在用 python 做一个面积计算器,一切似乎都很好,...直到我开始计算圆的周长...

谁能指出我正确的方向?

import math
from math import pi

menu = """
Pick a shape(1-3):
   1) Square (area)
   2) Rectangle (area)
   3) Circle (area)
   4) Square (perimeter)
   5) Rectangle (perimeter)
   6) Circle (perimeter)
   7) Quit
"""
shape = int(input(menu))
while shape != 7:
   if shape == 1:
      length = float(input("Length: "))
      print( "Area of square = ", length ** 2 )
   elif shape == 2:
      length = float(input("Length: "))
      width = float(input("Width: "))
      print( "Area of rectangle = ", length * width )  
   elif shape == 3:
      area = float(input("Radius: "))
      circumference = float(input("radius: "))
      print( "Area of Circle = ", pi*radius**2 )
   elif shape == 4:
      length = float(input("Length: "))
      print( "Perimeter of square = ", length *4 )
   elif shape == 5:
      length = float(input("Length: "))
      width = float(input("Width: "))
      print( "Perimeter of rectangle = ", (length*2) + (width*2))  
   elif shape == 6:
      circumference = float(input("radius: "))
      print( "Perimeter of Circle = ", 2*pi*radius)

   shape = int(input(menu))

【问题讨论】:

  • 你没有在任何地方指定半径......

标签: python calculator area


【解决方案1】:

您还应该考虑使用函数和 dict 结构,而不是使用 if .. elif 结构。它看起来像这样:

import math
from math import pi

def sq_area():
    length = float(input("Length: "))
    print( "Area of square = ", length ** 2 )

def sq_perim():
    length = float(input("Length: "))
    print( "Perimeter of square = ", length *4 )

def rect_area():
    length = float(input("Length: "))
    width = float(input("Width: "))
    print( "Area of rectangle = ", length * width )  

def rect_perim():
    length = float(input("Length: "))
    width = float(input("Width: "))
    print( "Perimeter of rectangle = ", (length*2) + (width*2))  

def cir_area():
    area = float(input("Radius: "))
    radius = float(input("radius: "))
    print( "Area of Circle = ", pi*radius**2 )

def cir_perim():
    radius = float(input("radius: "))
    print( "Perimeter of Circle = ", 2*pi*radius)

def bye():
    print("good-bye")

def unrec():
    print('Unrecognized command')

menu = """
   1) Square (area)
   2) Rectangle (area)
   3) Circle (area)
   4) Square (perimeter)
   5) Rectangle (perimeter)
   6) Circle (perimeter)
   7) Quit
Pick a shape(1-7):"""

shape = ''
while shape != '7':
   shape = raw_input(menu)
   {'1': sq_area,
    '2': rect_area,
    '3': cir_area,
    '4': sq_perim,
    '5': rect_perim,
    '6': cir_perim,
    '7': bye}.get(shape, unrec)()

【讨论】:

    【解决方案2】:

    将变量圆周替换为半径。

    【讨论】:

    • 虽然在技术上是正确的......他的圆形盒子还有其他问题;P
    【解决方案3】:

    是的,改变:

    elif shape == 6:
      **circumference** = float(input("radius: "))
      print( "Perimeter of Circle = ", 2*pi*radius)
    

    elif shape == 6:
      **radius** = float(input("radius: "))
      print( "Perimeter of Circle = ", 2*pi*radius)
    

    (强调)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-22
      • 1970-01-01
      • 2022-12-17
      • 2016-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多