312-9 Lists and loops error question

Hey @willx and @april.g, I got stucked at this point. it just kept on showing error. Please I need your help with the code:
row_1 = [‘Facebook’, 0.0, ‘USD’, 2974676, 3.5]
row_2 = [‘Instagram’, 0.0, ‘USD’, 2161558, 4.5]
row_3 = [‘Clash of Clans’, 0.0, ‘USD’, 2130805, 4.5]
row_4 = [‘Temple Run’, 0.0, ‘USD’, 1724546, 4.5]
row_5 = [‘Pandora - Music & Radio’, 0.0, ‘USD’, 1126879, 4.0]

app_data_set=[row_1, row_2, row_3, row_4, row_5]
rating_sum = 0
for row in app_data_set:
rating = row[-1]
rating_sum = rating_sum+rating
print(rating_sum)
avg_rating = rating_sum/len(app_data_set)

Output
File “”, line 10
rating = row[-1]
^
SyntaxError: invalid character in identifier

Hey @muhammedjimoh47 , welcome to the community! I went ahead and split your question into its own topic since it refers to a different mission than where you originally posted.

Looking at the code, is there indenting in your loop? When copying and pasting into the forums, unless the code is between triple backticks (```), it loses the formatting and indenting. If you could edit the post it would help me see better what the problem might be. Thanks!

can you please explain what the triple backticks do or mean

Sure! The forums here use Markdown formatting, so if you put triple backticks before and after the code (see the image below), it will be formatted the way you copied it from your code window. (The backtick ` key is usually to the left of the 1 key on most keyboards.)
This:
image

Turns into this:

# code sample
for item in alist:
    print(item)

okay. Thanks. I will do that soonest

It seems the code entered needs to exactly match The Answer in order to validate on this step. This problem is compounded by The Answer including print(rating_sum) within the for loop, but printing that variable was not one of the Instructions.

The Answer

row_1 = ['Facebook', 0.0, 'USD', 2974676, 3.5]
row_2 = ['Instagram', 0.0, 'USD', 2161558, 4.5]
row_3 = ['Clash of Clans', 0.0, 'USD', 2130805, 4.5]
row_4 = ['Temple Run', 0.0, 'USD', 1724546, 4.5]
row_5 = ['Pandora - Music & Radio', 0.0, 'USD', 1126879, 4.0]

app_data_set = [row_1, row_2, row_3, row_4, row_5]
rating_sum = 0
for row in app_data_set:
    rating = row[-1]
    rating_sum = rating_sum + rating
    print(rating_sum)
    
avg_rating = rating_sum / len(app_data_set)

Any deviation, however minor, results in an (unhelpful) error. For instance, an extra blank line before the for loop will cause an error on submission:

row_1 = ['Facebook', 0.0, 'USD', 2974676, 3.5]
row_2 = ['Instagram', 0.0, 'USD', 2161558, 4.5]
row_3 = ['Clash of Clans', 0.0, 'USD', 2130805, 4.5]
row_4 = ['Temple Run', 0.0, 'USD', 1724546, 4.5]
row_5 = ['Pandora - Music & Radio', 0.0, 'USD', 1126879, 4.0]

app_data_set = [row_1, row_2, row_3, row_4, row_5]
rating_sum = 0

for row in app_data_set:
    rating = row[-1]
    rating_sum = rating_sum + rating
    print(rating_sum)
    
avg_rating = rating_sum / len(app_data_set)

Errors:

  • avg_rating isn’t defined in your code, but we expected it to be float type
  • rating_sum isn’t defined in your code, but we expected it to be float type

That does seem weird, @ncauldwell. I’m not getting that same error when copying and pasting the code, and the code I had written to pass the mission didn’t include the print() command either. I know that this can happen if the virtual machine isn’t connected when you try to submit the code to be graded, so that’s usually the first thing I check. I’ll tag @Sahil on this post to see if this is a different issue.

I was getting a weird error on the next step (312-10) as well, with the same “solution” of copy/pasting The Answer. So I closed/re-opened my browser and my working code now submits successfully even if it is not an exact match. Before restarting the browser, I had tried doing a full page refresh but that did not fix the problem.
So whatever my problem was, it was not related to the validation code. And based on your response it was most likely that the VM wasn’t connected.
Thanks!

1 Like