打印从零到输入的所有数字

时间:2017-01-12 08:49:55

标签: python python-3.x typeerror

 def func(i):
    numbers=[]
    for x in range(0,i):
        print "At the top x is %d" %x
        numbers.append(x)
        x=x+1
        print "At the bottom x is %d" %x

    print "The numbers:"
    for i in numbers:
        print i

print "I'm going to print the numbers!"
i=raw_input("Enter the number: ")
func(i)

我使用python 3.0并使用textwrangler和终端进行编码。该代码应该打印从零到输入的所有数字。

3 个答案:

答案 0 :(得分:2)

  

我正在使用python 3.0

再次检查。因为你展示的代码显然是Python 2.x

要使代码在Python 3.x中运行,您需要进行一些修改:

  • print是一个功能。
  • raw_input已重命名为input

这会导致你的错误。 raw_input(或Python 3.x中的input)的返回值是一个字符串,而您期望range(0,i)中的整数。您需要使用int进行转换。

答案 1 :(得分:0)

首先,您作为用户输入所使用的值必须为int,因为您要打印数字。其次,raw_input()不适用于python 3.5.2,所以我使用了input()。下面的代码可以使用python 3.5.2。尝试一下。

    def func(i):
   numbers=[]
   for x in range(0, i):
       print ("At the TOP x is %d" %x)
       numbers.append(x)
       x=x+1
       print ("At the bottom x is %d" %x)

   print ("The numbers:")
   for i in numbers:
       print (i)

print ("I'm going to print the numbers!")
i = int(input("Enter the number: "))
func(i)

答案 2 :(得分:0)

尝试使用此 print(*list(range(int(input())),sep='')

相关问题