错误“以 10 为基数的 int() 的文字无效:”不断出现
我正在尝试编写一个非常简单的程序,我想打印出低于 100 的 3 和 5 的所有倍数之和,但是,错误不断出现,提示“以 10 为基数的 int() 的文字无效:”程序如下:
sum = ""
sum_int = int(sum)
for i in range(1, 101):
if i % 5 == 0:
sum += i
elif i % 3 == 0:
sum += i
else:
sum += ""
print sum
任何帮助将不胜感激。
I'm trying to write a very simple program, I want to print out the sum of all the multiples of 3 and 5 below 100, but, an error keeps accuring, saying "invalid literal for int() with base 10:" my program is as follows:
sum = ""
sum_int = int(sum)
for i in range(1, 101):
if i % 5 == 0:
sum += i
elif i % 3 == 0:
sum += i
else:
sum += ""
print sum
Any help would be much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
""
是导致这些问题的原因。更改
为
并摆脱
The
""
are the cause of these problems.Change
to
and get rid of
Python 不是 JavaScript:
""
不会自动转换为0
,0
也不会自动转换为"0".
您的程序似乎还混淆了打印三和五的所有倍数的总和以及打印三和五的倍数的所有数字的列表。
Python is not JavaScript:
""
does not automatically convert to0
, and0
does not automatically convert to"0"
.Your program also seems to be confused between printing the sum of all the multiples of three and five and printing a list of all the numbers which are multiples of three and five.
好吧,我是 Python 新手,所以我做了很多愚蠢的事情;不管怎样,我想我现在已经解决了。
Ok, I'm new to Python so I was doing quite a few silly things; anyway, I think I've worked it out now.