1

okay so i'm making code for my girlfriend for our 6th anniversary. I'm a complete noob to programming. I'm writing some very simple code to make, basically an input output machine of number inputs, in order for the user (her) to receive string outputs.

i keeps seeing "none" when i run my code. why? here goes.

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 "
4

2 回答 2

7
love(0) # is all you need.

你不需要打电话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" 

虽然,除此之外2,打印所有内容都不会受到伤害;)

我对 SO的第 100个答案!耶 !

于 2013-03-17T03:05:35.407 回答
1

你的函数有一个默认的返回值None,所以当你print输出它时,它会打印出来None

只需调用没有print语句的函数。

或者,您可以将print函数中的所有语句替换为return,并将其变成一个if-elif-else块,因为它们都是互斥操作。然后,打印love(0)实际上会打印出返回值。

于 2013-03-17T03:05:26.133 回答