如何在Python中添加变量?
Python noob here。我试图添加一组在'if'语句中定义的输入变量,并且每当我尝试查找总和时,它将只显示内联值。例如,当A,B,c和d等于5,周长= 555 ...如何在Python中添加变量?
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
str
值可以被添加到彼此很像号码。您使用的+
运算符可以正常工作,但会连接字符串的值。的raw_input
结果是一个字符串(str
),所以这就是为什么你会在15代替综上所述号码,使用int()
加入他们之前强迫为数字,看到'555'
:
try:
a = int(raw_input('gimme a number'))
except ValueError as e
print 'that was not a number, son'
感谢您的解释。虽然我希望自己有更好的方法来清理它,但它确实有帮助。哦,虽然,继承人是一个新手! – particlepat 2013-05-01 12:49:13
变量a
,b
,c
和d
,使用input(prompt)
而不是。 raw_input
返回一个字符串, 但input
返回作为python文字计算的控制台输入。 (截至目前,你是连接字符串的 ,而不是整数)。
确保您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."
你的代码是不是一个很好的设计。如果你想添加更多的形状,六角形,八角形等等。你实际上可以使用一个字典存储形状映射到边数。您不必为每个形状编写多个if语句。你将不得不做更少的类型检查,你可以使用python builtin函数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)
我是用字典做的。
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
我觉得字典会更容易使用。可能会更清洁,而不是重复自己。
http://www.programmingforums.org/thread22164.html – 2013-05-01 06:54:21
你缺少一个用于加法的int()。 – squiguy 2013-05-01 06:54:50