如何使我的代码再次询问用户输入python

时间:2017-04-24 02:48:46

标签: python

我正在创建一个python程序,为您输入的任何形状提供区域,我想知道如何使程序返回询问用户他们想要的形状

import math

user_choice = input("Choose a shape")

if user_choice == "rectangle" or "square":
    a = input("enter your length in centimeters here")
    b = input("enter your width in centimeters here")

    area = int(a) * int (b)
    print(area, "cm²")
else
    print"Sorry, you may have made a spelling error, or have chose a shape that we cannot calculate. Please try again"
#Code that returns to first question here?

如果可能的话,因为代码很长,因为它只是将if语句重复为新形状的elif语句,有没有办法缩短它,所以它不是很多elif语句。

由于

1 个答案:

答案 0 :(得分:-1)

import math

done = 'n'
while done == 'n':
     user_choice = input("Choose a shape")

     if user_choice == "rectangle" or "square":
         a = input("enter your length in centimeters here")
         b = input("enter your width in centimeters here")

         area = int(a) * int (b)
         print(area, "cm²")
     else
         print("Sorry, you may have made a spelling error, or have chose a shape that we cannot calculate. Please try again")
     done = input("Done? (Y/N)").lower()