简体   繁体   English

Python for循环返回NaN而不是字符串输出

[英]Python for loop returns NaN instead of string output

def numberToText(number):
     if number == 1:
         return('one')
     elif number == 2:
         return('two')
     elif number == 3:
         return('three')
     elif number == 4:
         return('four')
     elif number == 5:
         return('five')
     elif number == 6:
         return('six')
     elif number == 7:
         return('seven')
     elif number == 8:
         return('eight')
     elif number == 9:
         return('nine')
     elif number == 10:
         return('ten')

def tenGreenBottles():
  print ("How many bottles to start with?")
  bottles = int(input())
  for i in range (bottles, 0, -1):
      print "%g green bottles, hanging on the wall" %numberToText(i)
      print "%g green bottles, hanging on the wall" %numberToText(i)
      print "and if one green bottle, should accidentally fall"
      bob = i - 1
      print "there'd be %g green bottles." %numberToText(bob)
      print

I have been playing around with this code for a while and no matter what I do the For Loop prints out NaN instead of the string. 我一直在玩这段代码一段时间,无论我做什么,For Loop都会打印出NaN而不是字符串。 The function numberToText works as intended when I use the For Loop with out the string formatting. 当我不使用字符串格式使用For循环时,函数numberToText可以按预期工作。

You should try this instead: 您应该尝试以下方法:

 print "{} green bottles, hanging on the wall".format(numberToText(i))

My Reasoning for Recommending format 我的推荐format

Originally, in Python the common way to perform string interpolation (which is what we're talking about here) was to use format strings (such as %s , %d , or %g ) and a format operator between your string and the things that were to be inserted into it. 最初,在Python中执行字符串插值的常用方法(这就是我们在这里所说的)是在字符串和事物之间使用格式字符串(例如%s%d%g )和格式运算符插入其中。

However, you have to use the correct format string for the kind of thing you want to interpolate. 但是,对于要插值的事物,必须使用正确的格式字符串。

For instance, to insert a string into another string, you use %s : 例如,要将一个字符串插入另一个字符串,可以使用%s

>>> print "%s green bottles, hanging on the wall" % "fifteen"
fifteen green bottles, hanging on the wall

However, if you have a number you wish to interpolate, you would typically use %d : 但是,如果您想插入一个数字,通常会使用%d

>>> print "%d green bottles, hanging on the wall" % 15
15 green bottles, hanging on the wall

If you make a mistake, you get an error: 如果您犯了一个错误,则会得到一个错误:

>>> print "%d green bottles, hanging on the wall" % "15"
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: %d format: a number is required, not str

You seemed like you are new to Python, so I recommend just using format instead, which is a newer and more powerful way to perform string interpolation: 您似乎是Python的新手,所以我建议您只使用format ,这是一种执行字符串插值的更新且功能更强大的方法:

 >>> print "{} green bottles, hanging on the {}".format(20, "balcony")
 20 green bottles, hanging on the balcony

When you get more comfortable with string interpolation, you can try to do more advanced stuff with format . 当您对字符串插值更加满意时,可以尝试使用format做更多高级的事情。 You will probably never need to use % again to perform string interpolation. 您可能再也不需要使用%来执行字符串插值了。 I recommend just trying to focus on and remember format . 我建议只尝试着重记住format

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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