Type Conversion Errors in Project 1 and project 2

I was unable to convert strings to Int and float in both of these projects. please help me.

Regards,
Srikanth.

1 Like

Hi @SrikanthKumar,

I would like to take a look at your Guided Project, please click the Download button at the top of the Jupyter Notebook interface in our platform.

jupyter_guided_project_download.png

This will download a .tar file which contains both your notebook file and the required datasets. Please send me this .tar file.

Just as a side note, Do you know we have solutions for all the Jupyter based Guided Projects? You can access it by clicking the key-shaped icon.

Best,
Sahil

it seems like you are trying to convert the string ‘3.0M’ to float
you will have to remove M from the string before casting the string to float
One possible solution check for M every time in app[3] string and remove M then cast to float

for app in android:
…price = app[3]
…if ‘M’ in price:
------price = price.replace(‘M’, ‘’)
…price = float(price)

Sorry about indentation
I don’t know how to format as code

1 Like

Hi Sahil,
I have attached both .tar files of my projects 1 and 2. please find the attachments.

Thanks,
Srikanth.

Guided Project_ Exploring Hacker News Posts (1).tar (3.0 MB) Guided Project_ Profitable App Profiles for the App Store and Google Play Markets.tar (2.0 MB)

Hey,
Thanks for the idea. I have written this code and it is working for my project 1.
Code:
reviews_max = {}

for app in android:
name = app[0]
n_reviews = app[3]
str=‘3.0M’
if str in n_reviews:
n_reviews=str.replace(“M”,"")

n_reviews=float(n_reviews)

if name in reviews_max and reviews_max[name] < n_reviews:
    reviews_max[name] = n_reviews
    
elif name not in reviews_max:
    reviews_max[name] = n_reviews

But I was unable to resolve this issue in project 2.I have attached a screen shot of that error. Please help me out.

Thanks,
Srikanth.

Can you please show what is stored in ask_posts & show_posts

Hi @SrikanthKumar,

In the Guided Project: Profitable App Profiles for the App Store and Google Play Markets, you are getting that error because you forgot to run this code:

del(android[10472])

Since you have placed the above code on a cell above the code that reads the CSV files, I assume you might have accidentally missed running that code. I recommend you to place it in a cell below the code that reads CSV file to avoid this issue in the future.

In the Guided Project: Exploring Hacker News Posts, here:

ask_posts=[]
show_posts=[]
other_posts=[]

for row in hn:
    title=row[1]
    if title.lower().startswith('ask hn'):
        ask_posts.append(title)
    elif title.lower().startswith('show hn'):
        show_posts.append(title)
    else:
        other_posts.append(title)
        
print(len(ask_posts))
print(len(show_posts))
print(len(other_posts))

You are trying to append title instead of row. When you fix that, you will get an error here:

total_ask_comments=0
for row in ask_posts:
    comments=int(row[4])
    total_ask_comments=sum(comments)
    
avg_ask_comments=total_ask_comments/len(total_ask_comments)

print(avg_ask_comments)
TypeError                                 Traceback (most recent call last)
<ipython-input-5-ce8082468e87> in <module>
      2 for row in ask_posts:
      3     comments=int(row[4])
----> 4     total_ask_comments=sum(comments)
      5 
      6 avg_ask_comments=total_ask_comments/len(total_ask_comments)

TypeError: 'int' object is not iterable
total_show_comments=0
for row in show_posts:
    comments=int(row[4])
    total_show_comments=sum(comments)
    
avg_show_comments=total_show_comments/len(total_show_comments)

print(avg_show_comments)
TypeError                                 Traceback (most recent call last)
<ipython-input-6-aef7af6ffb05> in <module>
      2 for row in show_posts:
      3     comments=int(row[4])
----> 4     total_show_comments=sum(comments)
      5 
      6 avg_show_comments=total_show_comments/len(total_show_comments)

TypeError: 'int' object is not iterable

In both cases, you are getting the error on sum(comments). This is because the sum function iterates through an iterable (list, tuple, etc) to find the sum. However, comments is an integer. So you are getting TypeError: 'int' object is not iterable.

Please change = sum(comments) in both codes to += comments to fix this error.

Also, you will get TypeError: object of type 'int' has no len(). This is because you are using len(total_ask_comments) and len(total_show_comments). Please change it to len(ask_posts) and len(show_posts).

Hope this helps :slightly_smiling_face:. Let me know if you have any further question on it. I would be happy to help you.

Best,
Sahil

Hey Sahil,
I have figured it out. Thanks for your help.

Best,
Srikanth.

1 Like