python – ValueError:基数为10的int()的无效文字
|
我编写了一个程序来解决y = a ^ x然后将其投影到图表上.问题是每当一个< 1我收到错误:
有什么建议? 这是追溯: Traceback (most recent call last): File "C:UserskasutajaDesktopEksponentfunktsioonTEST - 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^x
import time
import math
import sys
import os
import subprocess
import matplotlib.pyplot as plt
print ("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:Userskasutajadesktopgraph.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:Userskasutajadesktopgraph.png")
解决方法
回答:
你的回溯告诉你int()采用整数,你试图给出一个小数,所以你需要使用float(): a = float(a) 这应该按预期工作: >>> int(input("Type a number: "))
Type a number: 0.3
Traceback (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.3
0.3
计算机以各种不同的方式存储数字. Python有两个主要的.整数,存储整数(?)和浮点数,存储实数(?).您需要根据需要使用正确的. (作为一个注释,Python非常擅长从你那里抽象出来,大多数其他语言也有双精度浮点数,例如,你不需要担心.从3.0开始,Python也会自动转换整数如果你把它们分开来漂浮,所以它实际上很容易使用.) 在我们进行追溯之前,先前猜测了答案: 您的问题是,无论您输入的是什么,都无法转换为数字.这可能是由许多事情引起的,例如: >>> int(input("Type a number: "))
Type a number: -1
-1
>>> int(input("Type a number: "))
Type a number: - 1
Traceback (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 – 如何确保函数在Go中占用一定的时间?
- python – 如果为true:destruct Class
- python删除过期文件的方法
- python – 无限循环服务GPIO的效率
- python – OSError:[WinError87]参数不正确
- 从Django 1.4升级到Django 1.7 – 它会起作用吗?
- Redland的Python绑定存储事务?
- 【Python】Python-numpy逻辑报错:The truth value of an a
- python – 使用httplib2.Http()对象时的最佳实践
- python – 启动py.test后,在读取请求的数据流后无法访问正
