Python Forum

Full Version: Beginner mistake.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello, I am a beginer learning on dataquest and I am stucked with a simple exercice where I make a mistake where I have my table new title but not the text values and I do not see where I do the mistake. Can someone help me ? Thank you. Bruno
opened_file = open('AppleStore.csv')
from csv import reader
read_file = reader(opened_file)
apps_data = list(read_file)

for app in apps_data[1:]:
    price = float(app[4])
    
    if price == 0.0:
        app.append('free')
    elif price > 0.0 and price < 20:
        app.append('affordable')
    elif price >= 20 and price < 50:
        app.append('expensive')
    else:
        app.append('very expensive')
    
apps_data_title = apps_data[0]
apps_data_title.append('price_label')
apps_dataprint = list (apps_data[0:6])
print(apps_dataprint)
You append to variable which is overwritten in every loop. From for-loop documentation (>>> help('for'))

Quote:The for-loop makes assignments to the variables(s) in the target list.
This overwrites all previous assignments to those variables including
those made in the suite of the for-loop:

   for i in range(10):
       print(i)
       i = 5             # this will not affect the for-loop
                         # because i will be overwritten with the next
                         # index in the range

In your code:

for app in apps_data[1:]:
/../
    app.append('free')
    app.append('affordable')
    app.append('expensive')
    app.append('very expensive')