在for循环中使用python中的整数参数(Using integer argument in python for a for loop)
新手到python这里,我试图用一个整数作为for循环的参数。 我使用的代码
from sys import argv script, numberofposts = argv postsint = int(numberofposts) for x in range(0,"%d" % postsint): print 'on time' print "number %d" % postsint给我这个错误 -
Traceback (most recent call last): File "forarg.py", line 6, in <module> for x in range(0,"%d" % postsint): TypeError: range() integer end argument expected, got str.我在这里做错了什么? 我认为这是一个语法问题,但是这个错误似乎表明for循环正在等待一个我试图强制的整数,正如你所看到的。
Newbie to python here, I am trying to use an integer as an argument for a for loop. The code I use
from sys import argv script, numberofposts = argv postsint = int(numberofposts) for x in range(0,"%d" % postsint): print 'on time' print "number %d" % postsintGives me this error -
Traceback (most recent call last): File "forarg.py", line 6, in <module> for x in range(0,"%d" % postsint): TypeError: range() integer end argument expected, got str.What am I doing wrong here? I assumed that it was a syntax issue but the error seems to indicate that the for loop is expecting an integer which I attempted to force as you can see.
最满意答案
for x in range(postsint): # start at beginning as mgilson suggested
应该工作正常。 你已经铸造了一个整数,你不应该从它创建一个字符串
for x in range(postsint): # start at beginning as mgilson suggested
should work fine. You already casted it an integer, You shouldn't be creating a string from it
更多推荐
发布评论