【问题标题】:How do I avoid getting a memory error in python?如何避免在 python 中出现内存错误?
【发布时间】:2020-07-13 19:47:09
【问题描述】:
Traceback (most recent call last):
  File "C:\Users\user\Desktop\modular inverse.py", line 12, in <module>
    Multiples_of_e.append(e*f)
MemoryError

这是代码:

Multiples_of_e = []

e=input("Enter the exponent: ")

f=1

for i in range(100000):
    Multiples_of_e.append(e*f)
    f+=1

如果我删除这两行

e=input("Enter the exponent: ")
totent=input("Enter the totent: ")

我只是在问题得到解决的代码中用它的值替换每个变量,有没有办法让这个工作不这样做?

【问题讨论】:

  • 问题显然出在第一个循环中,如错误消息所示。第二个循环无关紧要,将其从问题中删除。出于同样的原因,还要删除 totent=input("Enter the totent: ")foundit=False。现在,您输入的e 的值是多少?
  • 我通常输入的 e 的值是 65537,但即使它很大,只要我删除代码行 e=input("Enter the exponent: ")
  • 如果你插入一个简单的print 命令来跟踪你的值,你会发现问题。

标签: python python-3.x memory out-of-memory


【解决方案1】:

您忘记将输入转换为整数。您的循环将字符串相乘。

【讨论】:

    猜你喜欢
    • 2013-06-20
    • 2023-03-18
    • 1970-01-01
    • 2019-01-07
    • 1970-01-01
    • 2013-05-31
    • 1970-01-01
    • 1970-01-01
    • 2015-04-18
    相关资源
    最近更新 更多