if语句取决于输入

时间:2013-04-25 13:54:40

标签: python input

我正在尝试创建一种“简单”的方式来询问一个人他们希望找到哪个形状的区域。然后根据输入找到形状区域。我使用的是Python 2.7.3。以下是我到目前为止的情况:

from math import pi
c = ""
r = ""
x = (input("Do you have a [r]ectangle or a [c]ircle? ")) # Answer with r or c
if x == "r":
    l = (int(input("What is the length of your rectangle? ")))
    w = (int(input("What is the width of your rectangle? ")))
    print( l * w )
elif x == "c":
    r = (int(input("What is the radius of your circle? ")))
    print( r ** 2 * pi)
else:
    print("Please enter request in lower case. ")

1 个答案:

答案 0 :(得分:0)

from math import pi
# use raw input (raw_input()) for the inputs... input() is essentially eval(input())
# this is how i would ask for input for a simple problem like this
x = (raw_input("Do you have a [r]ectangle or a [c]ircle? ")) # Answer with r or c
# use .lower() to allow upper or lowercase
if x.lower() == "r":
    l = (int(raw_input("What is the length of your rectangle? ")))
    w = (int(raw_input("What is the width of your rectangle? ")))
    print( l * w )
elif x.lower() == "c":
    r = (int(raw_input("What is the radius of your circle? ")))
    print( r ** 2 * pi)
else:
    print("You didn't enter r or c.")