python – ValueError:基数为10的int()的无效文字

python – ValueError:基数为10的int()的无效文字,第1张

概述我编写了一个程序来解决y = a ^ x然后将其投影到图表上.问题是每当一个< 1我收到错误: ValueError: invalid literal for int () with base 10. 有什么建议? 这是追溯: Traceback (most recent call last): File "C:\Users\kasutaja\Desktop\Eksponentfunkts 我编写了一个程序来解决y = a ^ x然后将其投影到图表上.问题是每当一个< 1我收到错误:

ValueError: invalID literal for int () with base 10.

有什么建议?

这是追溯:

Traceback (most recent call last):    file "C:\Users\kasutaja\Desktop\EksponentfunktsioonTEST - koopia.py",line 13,in <module>    if int(a) < 0: ValueError: invalID literal for int() with base 10: '0.3'

每次我放一个小于1但大于0的数字时就会出现问题.对于这个例子,它是0.3.

这是我的代码:

#  y = a^ximport timeimport mathimport sysimport osimport subprocessimport matplotlib.pyplot as pltprint ("y = a^x")print ("")a = input ("Enter 'a' ")print ("")if int(a) < 0:    print ("'a' is negative,no solution")elif int(a) == 1:    print ("'a' is equal with 1,no solution")else:    fig = plt.figure ()    x = [-2,-1.75,-1.5,-1.25,-1,-0.75,-0.5,-0.25,0.25,0.5,0.75,1,1.25,1.5,1.75,2]    y = [int(a)**(-2),int(a)**(-1.75),int(a)**(-1.5),int(a)**(-1.25),int(a)**(-1),int(a)**(-0.75),int(a)**(-0.5),int(a)**(-0.25),int(a)**(0),int(a)**(0.25),int(a)**(0.5),int(a)**(0.75),int(a)**1,int(a)**(1.25),int(a)**(1.5),int(a)**(1.75),int(a)**(2)]    ax = fig.add_subplot(1,1)    ax.set_Title('y = a**x')    ax.plot(x,y)    ax.spines['left'].set_position('zero')    ax.spines['right'].set_color('none')    ax.spines['bottom'].set_position('zero')    ax.spines['top'].set_color('none')    ax.spines['left'].set_smart_bounds(True)    ax.spines['bottom'].set_smart_bounds(True)    ax.xaxis.set_ticks_position('bottom')    ax.yaxis.set_ticks_position('left')    plt.savefig("graph.png")    subprocess.Popen('explorer "C:\Users\kasutaja\desktop\graph.png"')def restart_program():     python = sys.executable    os.execl(python,python,* sys.argv)if __name__ == "__main__":    answer = input("Restart program? ")    if answer.strip() in "YES yes Yes y Y".split():        restart_program()    else:        os.remove("C:\Users\kasutaja\desktop\graph.png")
解决方法 回答:

你的回溯告诉你int()采用整数,你试图给出一个小数,所以你需要使用float():

a = float(a)

这应该按预期工作:

>>> int(input("Type a number: "))Type a number: 0.3Traceback (most recent call last):  file "<stdin>",line 1,in <module>ValueError: invalID literal for int() with base 10: '0.3'>>> float(input("Type a number: "))Type a number: 0.30.3

计算机以各种不同的方式存储数字. Python有两个主要的.整数,存储整数(ℤ)和浮点数,存储实数(ℝ).您需要根据需要使用正确的.

(作为一个注释,Python非常擅长从你那里抽象出来,大多数其他语言也有双精度浮点数,例如,你不需要担心.从3.0开始,Python也会自动转换整数如果你把它们分开来漂浮,所以它实际上很容易使用.)

在我们进行追溯之前,先前猜测了答案:

您的问题是,无论您输入的是什么,都无法转换为数字.这可能是由许多事情引起的,例如:

>>> int(input("Type a number: "))Type a number: -1-1>>> int(input("Type a number: "))Type a number: - 1Traceback (most recent call last):  file "<stdin>",in <module>ValueError: invalID literal for int() with base 10: '- 1'

在 – 和1之间添加空格将导致字符串无法正确解析为数字.当然,这只是一个例子,您必须告诉我们您给我们的输入是什么,以便能够确定问题是什么.

关于代码风格的建议:

y = [int(a)**(-2),int(a)**(2)]

这是一个非常糟糕的编码习惯的例子.你在一次又一次地复制某些东西是错误的.首先,你使用int(a)很多次,无论你做什么,你都应该将值赋给变量,而是使用它来避免一次又一次地键入(并强制计算机计算)该值:

a = int(a)

在这个例子中,我将值赋值给a,用我们想要使用的新值覆盖旧值.

y = [a**i for i in x]

这段代码产生的结果与上面的怪物相同,没有大量的反复写出相同的东西.这是一个简单的list comprehension.这也意味着如果你编辑x,你不需要做任何事情,它会自然更新以适应.

另请注意PEP-8,the Python style guide,suggests strongly that you don’t leave spaces between an identifier and the brackets when making a function call.

总结

以上是内存溢出为你收集整理的python – ValueError:基数为10的int()的无效文字全部内容,希望文章能够帮你解决python – ValueError:基数为10的int()的无效文字所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1191115.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-03
下一篇 2022-06-03

发表评论

登录后才能评论

评论列表(0条)

保存