如何在Python中添加变量?

时间:2013-05-01 06:53:26

标签: python variables if-statement sum

Python noob在这里。我正在尝试添加一组在'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

5 个答案:

答案 0 :(得分: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'

答案 1 :(得分: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."

答案 2 :(得分:0)

对于变量abcd,请使用input(prompt)代替raw_input(prompt)raw_input返回一个字符串, 但是input返回被评估为python文字的控制台输入。 (截至目前,你是 连接字符串,而不是添加整数。)

答案 3 :(得分: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)

答案 4 :(得分: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 

我认为字典会更容易使用。可能会更清洁,而不是重复自己。