[英]Running python script in terminal, nothing prints or shows up - why?
[英]Why python script runs but nothing shows on screen
我是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]
缩进在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?"
您的缩进全部搞砸了。 在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?"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.