在for循环中使用python中的整数参数(Using integer argument in python for a for loop)

编程入门 行业动态 更新时间:2024-10-17 13:29:45
在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" % postsint

Gives 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

更多推荐

本文发布于:2023-04-28 06:15:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1330337.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:整数   参数   python   loop   argument

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!