Python:函数定义在所需输出的中间保持返回"None"

dav*_*wad 1 python

好的,所以我为我的女朋友为我们的6周年纪念做代码.我是编程的完整菜鸟.我正在编写一些非常简单的代码,基本上是数字输入的输入输出机器,以便用户(她)接收字符串输出.

当我运行我的代码时,我一直看到"无".为什么?开始.

def love(n):
  if n < 0 : 
    print "Why would it be negative?!" 
  if n == 0 : 
      print "well that is just hurtful" 
  if n == 1 :
    print "I REALLY love you" 
  if n == 2 : 
    print "You make me smile at least once, each and every day"
  if n == 3 : 
    print"you wouldn't believe how annoying it was to get this program to run properly! but it was worth it"
  if n == 4 : 
      print "let's " + "shoot a little higher than that"
  else:
    print "I honestly can't see myself without you anymore" 


print love(0) 

print "Wanna try again? :D "
Run Code Online (Sandbox Code Playgroud)

ash*_*shr 7

love(0) # is all you need.
Run Code Online (Sandbox Code Playgroud)

你不需要打电话print love(),因为你已经有打印声明love.你看到Nonelove是做了所有的工作,它不返回任何东西.


此外,您需要使用if-elif-else您只想块的功能一个,出所有的打印操作的,在一个时间运行.

if n < 0 : 
    print "Why would it be negative?!" 
elif n == 0 : 
      print "well that is just hurtful" 
elif n == 1 :
    print "I REALLY love you" 
elif n == 2 : 
    print "You make me smile at least once, each and every day"
elif n == 3 : 
    print"you wouldn't believe how annoying it was to get this program to run properly! but it was worth it"
elif n == 4 : 
      print "let's " + "shoot a little higher than that"
else:
    print "I honestly can't see myself without you anymore" 
Run Code Online (Sandbox Code Playgroud)

虽然,除此之外2,打印一切都不会受到伤害;)

关于SO,我的 100 答案!好极了 !