什么是ValueError?

时间:2018-08-07 20:18:19

标签: python

我有一个简单的python脚本,需要用户输入:

limit=int(input())
answer=1
a=[]
for i in range(limit):
    data=int(input())
    a.append(data)
for j in a:
    answer = answer*j+10**9+7
print(answer)

当我运行它时,我不断收到像这样的错误。

Execution failed.
ValueError: could not convert string to float : '1 2 3 4 5'

Stack Trace:
Traceback (most recent call last):
File "/hackerearth/PYTHON3_12/s_0b.py3", line 5, in 
data=int(float(input()))
ValueError: could not convert string to float: '1 2 3 4 5'

什么是ValueError?如何解决此问题?

1 个答案:

答案 0 :(得分:0)

您需要一次解析每个数字。您试图一次输入所有数字,这将不起作用。试试这个:

{
"CPU Package": [
    {
        "SensorType": "Power",
        "Value": 15.2162886
    },
    {
        "SensorType": "Temperature",
        "Value": 69
    }
],
"Temperature": [
    {
        "SensorType": "Temperature",
        "Value": 41
    }
],
"CPU Core #2": [
    {
        "SensorType": "Temperature",
        "Value": 69
    },
    {
        "SensorType": "Load",
        "Value": 60.15625
    },
    {
        "SensorType": "Clock",
        "Value": 2700.00073
    }
],
"CPU Core #1": [
    {
        "SensorType": "Temperature",
        "Value": 66
    },
    {
        "SensorType": "Clock",
        "Value": 3100.001
    },
    {
        "SensorType": "Load",
        "Value": 54.6875
    }
],
"CPU Cores": [
    {
        "SensorType": "Power",
        "Value": 13.3746643
    }
],
"Available Memory": [
    {
        "SensorType": "Data",
        "Value": 3.68930435
    }
],
"Used Space": [
    {
        "SensorType": "Load",
        "Value": 93.12801
    }
],
"Bus Speed": [
    {
        "SensorType": "Clock",
        "Value": 100.000031
    }
],
"Memory": [
    {
        "SensorType": "Load",
        "Value": 53.3276978
    }
],
"Used Memory": [
    {
        "SensorType": "Data",
        "Value": 4.215393
    }
],
"CPU Total": [
    {
        "SensorType": "Load",
        "Value": 57.421875
    }
],
"CPU DRAM": [
    {
        "SensorType": "Power",
        "Value": 1.05141532
    }
],
"CPU Graphics": [
    {
        "SensorType": "Power",
        "Value": 0.119861834
    }
]
}

我将您的代码更改为1)在循环前输入 ,然后2)将其分成一个列表,其中每个元素都是原始字符串中的数字之一,其中3)然后可以将其解析为循环中的数据。