2013-04-25 73 views
-1

我想创建一个“简单”的方式来问一个人他们想要找到什么形状的区域。然后根据他们的输入找出形状的区域。我正在使用Python 2.7.3。以下是我迄今为止:if-statement取决于输入

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

你所拥有的一切运作正常。只是像你这样的一些不必要的变量不需要'c =“''和'r =”“',你应该在你的'input'的末尾加上'.lower()',这样大写的响应仍然被接受 – 2013-04-25 14:22:51

+0

非常感谢你!现在就像魅力一样。 – 2013-04-25 15:05:13

+0

我只是不明白为什么我收到-1。 – 2013-11-13 15:44:50

回答

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

尽量养成不只是张贴代码的习惯,还要给出一个句子或2个解释。 – Dukeling 2013-04-25 14:01:07

+0

@Dukeling解释在代码 – brbcoding 2013-04-25 14:01:51

+0

中的注释中。相当多的括号可以从此代码中删除。不是让他们在那里是错误的,只是多余的。 – Aleph 2013-04-25 15:43:33