加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

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

发布时间:2020-12-20 10:34:52 所属栏目:Python 来源:网络整理
导读:我编写了一个程序来解决y = a ^ x然后将其投影到图表上.问题是每当一个 1我收到错误: ValueError: invalid literal for int () with base 10. 有什么建议? 这是追溯: Traceback (most recent call last): File "C:UserskasutajaDesktopEksponentfunkt
我编写了一个程序来解决y = a ^ x然后将其投影到图表上.问题是每当一个< 1我收到错误:

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

有什么建议?

这是追溯:

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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读