L oop для калькулятора формы - PullRequest
0 голосов
/ 21 февраля 2020

Мне нужно добавить в код al oop, чтобы пользователь мог перезапустить программу и выбрать другую из доступных фигур.

import math
import time
shapearea = 0
pi = 3.14159
print("Select shape\n1.Square\n2.Circle\n3.Triangle")
shape = input("Enter shape(Square/Circle/Triangle):")
if shape == "square":
     sideval = int(input("Enter side value of the shape:"))
     print("Calculating...")
     #time.sleep(2)
     shapearea = shapearea +(sideval ** 2)
elif shape == "circle":
     circleradius = int(input("Enter radius value of the circle:"))
     print("Calculating...")
     #time.sleep(2)
     shapearea = shapearea +(pi * circleradius **2)
elif shape == "triangle":
    height = int(input("Enter the height of the shape:"))
    base = int(input("Enter the base of the shape:"))
    print("Calculating...")
    #time.sleep(2)
    shapearea = shapearea +(base*height/2)
else:
     print("The given shape is invalid, give a valid shape to calculate the area")
print ("The area of the chosen shape is " "%.2f" % shapearea)

1 Ответ

0 голосов
/ 21 февраля 2020

попробуйте это, может быть, это может помочь вам:

import math
import time

def calculate():
    shapearea = 0
    pi = 3.14159
    print("Select shape\n1.Square\n2.Circle\n3.Triangle")
    shape = input("Enter shape(Square/Circle/Triangle):")
    if shape == "square":
         sideval = int(input("Enter side value of the shape:"))
         print("Calculating...")
         #time.sleep(2)
         shapearea = shapearea +(sideval ** 2)
    elif shape == "circle":
         circleradius = int(input("Enter radius value of the circle:"))
         print("Calculating...")
         #time.sleep(2)
         shapearea = shapearea +(pi * circleradius **2)
    elif shape == "triangle":
        height = int(input("Enter the height of the shape:"))
        base = int(input("Enter the base of the shape:"))
        print("Calculating...")
        #time.sleep(2)
        shapearea = shapearea +(base*height/2)
    else:
         print("The given shape is invalid, give a valid shape to calculate the area")
    print ("The area of the chosen shape is " "%.2f" % shapearea)

calculate()

while input("Do you want to continue?(Y/N)") == "Y":
    calculate()
print("exiting from program...")
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...