为用户提供重新运行代码的选项

时间:2016-10-05 11:40:25

标签: python

print ("Welcome to my Area Calculator")
i=raw_input("Please enter the shape whose Area you want to calculate(square/rectangle/right angled triangle/rhombus/parallelogram): ")
my_list=["square","rectangle","triangle","right angled triangle","rhombus","parallelogram"]
if i in my_list:
   if i=="square":
       s=float(input("What is the side of the square: "))
       print "Area of the square is : ", s**2
   elif i=="rectangle":
       l=float(input("What is the length of the rectangle: "))
       b=float(input("What is the breadth of the rectangle: "))
       print "Area of the rectangle is : ", l*b

   elif i=="right angled triangle":
       base1=float(input("What is the base of the triangle: "))
       height1=float(input("What is the height of the triangle: "))
       print "Area of the triangle is : ", 0.5*base1*height1
   elif i=="rhombus":
       d1=float(input("What is the length of the 1st diagnol of the rhombus: "))
       d2=float(input("What is the length of the 2nd diagnol of the rhombus: "))
       print "Area of the rhombus is : ", 0.5*d1*d2
   elif i=="parallelogram":
       base=float(input("What is the length of 1 side of the parallelogram: "))
       height=float(input("What is the length of the other side: "))
       print "Area of the parallelogram is : ", height*base
   print "Thank you so much for using my area calculator"

2 个答案:

答案 0 :(得分:2)

让你的整个程序成为一个函数,然后在循环中执行它。

while(option != 'yes'):
   Program()
   userInput()

答案 1 :(得分:0)

询问用户是否要继续,将其放在布尔变量中,将所有代码放在while循环中,并将此布尔值作为条件
不要忘记将此布尔值初始化为true