I am trying to write some python code, that if there is only 1 term in a list, an error code is printed.
This is my code so far:
#code to put data into seperate lists
inputdata=open("C:\Users\Chloe\Google Drive\Uni\Computing\data.txt", 'r')
for datapoint in inputdata:
datapoint=datapoint.strip('\n')
splitdata=datapoint.split(',')
x.append(splitdata[0])
y.append(splitdata[1])
e.append(splitdata[2])
#converting data to float in lists
x=[float(i) for i in x]
y=[float(i) for i in y]
e=[float(i) for i in e]
print ('the x coordinates for the data points are '+str(x))
print ('the y coordinates for the data points are '+str(y))
print ('the associated error for the data points is '+str(e))
The code works when the correct data is given.
Do you mean:
splitdata=datapoint.split(',')
if len(splitdata) != 3:
print("Wrong number of things")
else:
x.append(splitdata[0])
y.append(splitdata[1])
e.append(splitdata[2])
or even:
if len(splitdata) != 3:
raise ValueError("Input data must have three items separated by commas.")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.