2013-05-20 2 views
0

Я работаю на калькуляторе области в питоне, и все, кажется, хорошо, ... пока я не вычислением периметра круга ...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)) 
+0

ваш не назначая радиус где-нибудь ... –

ответ

1

Заменить переменную окружность радиусом.

+0

в то время как технически правильно ... есть и другие проблемы, связанные с его окружностью случае, P –

0

Да, изменение:

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) 

(курсив добавлен)

+0

OMG !! Спасибо!!! – jeepjenn

1

Вы должны также рассмотреть возможность использования функций и структуры Dict вместо того, чтобы использовать, если .. Элиф структура , Это будет выглядеть следующим образом:

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)() 
Смежные вопросы