0

Python 菜鸟在这里。我正在尝试添加在“if”语句中定义的一组输入变量,每当我尝试找到总和时,它只会显示内联值。例如,当 a、b、c 和 d 等于 5 时,周长 = 555...

shape = raw_input("Is the plot a (q) quadrilateral or (t) triangle?")
if shape.lower() == "q":
    a = raw_input("What is the length in feet of side 'a'?")
    b = raw_input("What is the length in feet of side 'b'?")
    c = raw_input("What is the length in feet of side 'c'?")
    d = raw_input("What is the length in feet of side 'd'?")
elif shape.lower() == "t":
    a = raw_input("What is the length in feet of side 'a'?")
    b = raw_input("What is the length in feet of side 'b'?")
    c = raw_input("What is the length in feet of side 'c'?")
else:
    print "Please enter 'q' for quadrilateral or 't' for triangle."

if shape.lower() == "q":
    perimeter = a + b + c + d
elif shape.lower() == "t":
    perimeter = a + b + c
else:
    print "Please make sure you enter numbers only."

print perimeter
4

5 回答 5

2

str值可以像数字一样彼此相加。您使用的+运算符工作正常,但连接字符串的值。结果raw_input是一个字符串 ( str),这就是为什么您会看到'555'而不是 15。要对数字求和,请int()在将它们相加之前将值强制转换为数字:

try:
    a = int(raw_input('gimme a number'))
except ValueError as e
    print 'that was not a number, son'
于 2013-05-01T06:56:41.407 回答
1

确保您raw_input实际上是一个 int():

shape = raw_input("Is the plot a (q) quadrilateral or (t) triangle?")
if shape.lower() == "q":
    try:
        a = raw_input("What is the length in feet of side 'a'?")
        b = raw_input("What is the length in feet of side 'b'?")
        c = raw_input("What is the length in feet of side 'c'?")
        d = raw_input("What is the length in feet of side 'd'?")
        perimeter = int(a) + int(b) + int(c) + int(d)
    except ErrorValue as e  
        print "Please make sure you enter numbers only."

elif shape.lower() == "t":
    try:
        a = raw_input("What is the length in feet of side 'a'?")
        b = raw_input("What is the length in feet of side 'b'?")
        c = raw_input("What is the length in feet of side '
        perimeter = int(a) + int(b) + int(c)
    except ErrorValue as e  
        print "Please make sure you enter numbers only."
else:
    print "Please enter 'q' for quadrilateral or 't' for triangle."
于 2013-05-01T07:07:39.040 回答
0

对于变量a, b,cd, 使用input(prompt)代替raw_input(prompt). raw_input返回一个字符串,但input返回计算为 python 文字的控制台输入。(截至目前,您正在连接字符串,而不是添加整数)。

于 2013-05-01T07:03:50.763 回答
0

您的代码不是一个好的设计。如果你想添加更多的形状,六边形,八边形等等。您实际上可以使用 dict 将形状映射存储到边数。您不必为每个形状编写多个 if 语句。你将不得不做更少的类型检查,你可以使用 python 内置函数 sum 来返回参数。现在继续并尝试以下操作:

d = {'q': 4, 't': 3}

shape = raw_input("Is the plot a (q) quadrilateral or (t) triangle?\n")

if shape.lower() not in d:
    print "Please enter 'q' for quadrilateral or 't' for triangle."

else:
    sides = []

    for i in range(0,d.get(shape.lower())):
        side = raw_input("What is the length in feet of side " + str(i+1))
        try:
            sides.append(int(side))
        except ValueError:
            print "Integer value only"

    print sum(sides)
于 2013-05-01T07:50:45.373 回答
0

我用字典做了这个。

sides = {'a':0,'b': 0,'c': 0,'d': 0}
perimeter = 0

shape = raw_input("Is the plot a (q) quadrilatral or (t) triangle?: ")
if shape.lower() == "q":
    for side, length in sides.iteritems():
        sides[side] = input("What is the length (in feet) of side %s?: " % side)
        perimeter+=int(sides[side])

elif shape.lower() == "t":
    sides.pop("d",None)
    for side, length in sides.iteritems():
        sides[side] = input("What is the length (in feet) of side %s?: " % side)
        perimeter+=int(sides[side])
else:
    print "Please enter 'q' or 't'."

print "Perimeter is: %d" % perimeter 

我想字典会更容易使用。可能会更干净,而不是重复自己。

于 2013-05-01T08:30:58.280 回答