错误“以 10 为基数的 int() 的文字无效:”不断出现

发布于 2024-08-28 04:09:23 字数 304 浏览 8 评论 0原文

我正在尝试编写一个非常简单的程序,我想打印出低于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

陈年往事 2024-09-04 04:09:23

"" 是导致这些问题的原因。

更改

sum = ""

sum = 0

并摆脱

else:
 sum += ""

The "" are the cause of these problems.

Change

sum = ""

to

sum = 0

and get rid of

else:
 sum += ""
月亮坠入山谷 2024-09-04 04:09:23

Python 不是 JavaScript:"" 不会自动转换为 00 也不会自动转换为 "0".

您的程序似乎还混淆了打印三和五的所有倍数的总和以及打印三和五的倍数的所有数字的列表。

Python is not JavaScript: "" does not automatically convert to 0, and 0 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.

半仙 2024-09-04 04:09:23

好吧,我是 Python 新手,所以我做了很多愚蠢的事情;不管怎样,我想我现在已经解决了。

sum = 0
for i in range(1, 1001):
    if i % 5 == 0:
        sum += i 
    elif i % 3 == 0:
        sum += i

print sum

Ok, I'm new to Python so I was doing quite a few silly things; anyway, I think I've worked it out now.

sum = 0
for i in range(1, 1001):
    if i % 5 == 0:
        sum += i 
    elif i % 3 == 0:
        sum += i

print sum
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文