我是python编程的初学者,但我现在面临的挑战是,无论何时我运行包含屏幕上没有显示任何功能的脚本。
示例是以下代码:
def add(a,b):
print "Adding %d + %d" %(a, b)
return a +b
def subtract(a, b):
print "Subtracting %d - %d"%(a, b)
return a-b
def multiply(a, b):
print "Multiplying %d * %d" %(a, b)
return a * b
def divide(a, b):
print "Dividing %d / %d" %(a, b)
return a / b
print "Lets do some math with just functions!"
age = add(30,5)
height = subtract(78, 4)
weight = multiply(90, 2)
iq = divide(100, 2)
print "Age: %d, Height: %d, Weight: %d, IQ: %d"%(age,height,weight)
# A puzzle for the extra credit, type it in anyway.
print "Here is a puzzle."
what = add(age, subtract(height, multiply(weight, divide(iq, 2))))
print "That becomes: ", what, "Can you do it by hand?"[enter image description here][1]
答案 0 :(得分:2)
缩进在Python中非常重要(也很遗憾看到IQ设置为50)
def add(a,b):
print "Adding %d + %d" % (a, b)
return a + b
def subtract(a, b):
print "Subtracting %d - %d" % (a, b)
return a - b
def multiply(a, b):
print "Multiplying %d * %d" % (a, b)
return a * b
def divide(a, b):
print "Dividing %d / %d" % (a, b)
return a / b
print "Lets do some math with just functions!"
age = add(30,5)
height = subtract(78, 4)
weight = multiply(90, 2)
iq = divide(100, 2)
print "Age: %d, Height: %d, Weight: %d, IQ: %d" % (age, height, weight, iq)
# A puzzle for the extra credit, type it in anyway.
print "Here is a puzzle."
what = add(age, subtract(height, multiply(weight, divide(iq, 2))))
print "That becomes: ", what, "Can you do it by hand?"
答案 1 :(得分:0)
你的缩进都搞砸了。在Python中,缩进是告诉解释器代码应该在哪里,它表示一段代码是否应该在函数内部,例如。我也相信你的编辑有一个奇怪的问题。因为这是一个学习Python的艰难方式,你可能想要设置你的编辑器,就像作者在本书开头所说的那样。如果遗漏任何内容,请发表评论,我会更新我的答案!
def add(a, b):
print "ADDING %d + %d" % (a, b)
return a + b
def subtract(a, b):
print "SUBTRACTING %d - %d" % (a, b)
return a - b
def multiply(a, b):
print "MULTIPLYING %d * %d" % (a, b)
return a * b
def divide(a, b):
print "DIVIDING %d / %d" % (a, b)
return a / b
print "Let's do some math with just functions!"
age = add(30, 5)
height = subtract(78, 4)
weight = multiply(90, 2)
iq = divide(100, 2)
print "Age: %d, Height: %d, Weight: %d, IQ: %d" % (age, height, weight, iq)
# A puzzle for the extra credit, type it in anyway.
print "Here is a puzzle."
what = add(age, subtract(height, multiply(weight, divide(iq, 2))))
print "That becomes: ", what, "Can you do it by hand?"