-1

我正在尝试创建一种“简单”的方式来询问一个人他们想找到什么形状的区域。然后根据他们的输入找到形状的区域。我正在使用 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. ")
4

1 回答 1

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.")
于 2013-04-25T13:58:55.080 回答