[Tutor] Program help

Mark Lawrence breamoreboy at gmail.com
Fri Oct 23 15:15:10 EDT 2020


On 23/10/2020 19:25, alexkleider via Tutor wrote:
> ‐‐‐‐‐‐‐ Original Message ‐‐‐‐‐‐‐
> On Thursday, October 22, 2020 3:16 PM, nathan tech <nathan-tech at hotmail.com> wrote:
> 
>> Hi Garrett
>>
>> This reads rather like a homework assignment! :)
>>
>> Here are some tips that I hope may help:
>>
>> think through what you need to do:
>>
>> 1.  Load the data.
>> 2.  Do something with the data.
>> 3.  Display the data in some way.
>>
>>      the first bit is quite easy:
>>
>>      f=open("population.txt","r")
>>
>>      data=f.read()
>>
>>      f.close()
>>
>>      data=data.split("\n")
>>
>>      Convert it to numbers:
>>
>>      for x in range(data):
>>
>>       data[x]=int(data[x])
>>
>>      The rest is just some fancy footwork with a for loop like the one above
>>      and some organisation into variables depending on what you want to do
>>      with it.
>>
>>      Hope this helps!
>>
>>      Nathan
> 
> 
> The for loop didn't seem quite right to me since data appears to be a list.
> Python 3.7.3 (default, Jul 25 2020, 13:03:44)
> [GCC 8.3.0] on linux
> Type "help", "copyright", "credits" or "license" for more information.
>>>> d = ['1', '3', '4']
> 
>>>> for x in range(d):
> ...   data[x] = int(d[x])
> ...
> Traceback (most recent call last):
>    File "<stdin>", line 1, in <module>
> TypeError: 'list' object cannot be interpreted as an integer
>>>>
> 
> ... or am I missing something?
> 

No, the loop was clearly untested. Why not:-

 >>> data = ['1', '3', '4']
 >>> data = [int(x) for x in data]
 >>> data
[1, 3, 4]

-- 
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence



More information about the Tutor mailing list