2017-04-24 105 views
-1

我正在创建一个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的声明。

感谢

+4

提示:'while'循环。 – tadman

回答

-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() 
相关问题