I am attempting to make a string an integer by saying
file = open(input("Please enter the name of the file you wish to open:" ))
while True:
A = file.readline()
if(A):
array.append(int(A[0:len(A)-1]))
else:
break
print("The numbers in the file are:", A)
file.close()
The file I created has numbers: 1 -3 10 6 5 0 3 -5 20
Here is the error:
ValueError: invalid literal for int() with base 10: '1 -3 10 6 5 0 3 -5 2'
Read the error –
'1 -3 10 6 5 0 3 -5 2'is not a number. It’s alistof numbers. You need to turn it into a list of strings first.Also,you shouldn’t really use
.close(). Usewithinstead.