2013-04-25 65 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