Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 形状计算器循环_Python_Loops - Fatal编程技术网

Python 形状计算器循环

Python 形状计算器循环,python,loops,Python,Loops,我需要在代码中添加一个循环,以允许用户重新启动程序并选择其他可用形状 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 si

我需要在代码中添加一个循环,以允许用户重新启动程序并选择其他可用形状

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)

试试这个也许这个能帮你:

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...")

您的问题是什么?到目前为止您尝试了什么?将代码包装在
中,如果为True:
。。。别忘了在exitWelcome中添加一个选项以使堆栈溢出!查看和。这不是问题。请澄清您的问题。我尝试过使用while函数,但我以前没有做过循环,我不确定如何实现这一点