简体   繁体   English

为什么python脚本运行但屏幕上什么都没有显示

[英]Why python script runs but nothing shows on screen

I am a beginner in python programming but the challenge I am facing now is that anytime I run a script containing functions nothing shows on screen. 我是python编程的初学者,但是我现在面临的挑战是,无论何时我运行的脚本都包含屏幕上没有显示的功能。

Example is the code below: 示例如下代码:

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]

Indentation is very important in Python (also sorry to see that the IQ is set to 50) 缩进在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?"

Your indentation is all messed up. 您的缩进全部搞砸了。 In Python, indents are what tells the interpreter where the code should be in the sense that it says whether a piece of code should be inside a function or not for example. 在Python中,缩进是告诉解释器代码应该在哪里的意思,例如,它说出一段代码是否应该在函数内部。 I also believe you have a strange problem with your editor. 我也相信您的编辑器有一个奇怪的问题。 As this is a Learn Python the Hard Way you may want to setup your editor like the author says in the start of the book. 由于这是“学习Python的艰难方式”,因此您可能希望像作者在本书开始时所说的那样设置编辑器。 Please comment if anything is missing and I'll update my answer! 如果有任何遗漏,请发表评论,我将更新答案!

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM