0

Please check my code here: Sample url: http://py4e-data.dr-chuck.net/comments_42.html Sum of the digits found in following url should be (2553). I have to tried to sum up using several techs but can't find the correct one use the url provided at the top of the code. I need to sum up the strings numbers.

import urllib
from urllib.request import urlopen
from bs4 import BeautifulSoup
import ssl

# Ignore SSL certificate errors
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE

# To read the file from the url
url = input('Enter - ')
html = urllib.request.urlopen(url, context=ctx).read()
soup = BeautifulSoup(html, "html.parser")

# To search for specific area of the file
tags = soup('span')
#print(tags)
sum = 0

# Filters your search further and prints the specific part as                     
#string
for tag in tags:
    print(tag.contents[0])
    #ChangeToInt = int(tag.contents[0])
    #sum =+ ChangeToInt
    #print(sum)
Sixcheviexx
  • 11
  • 1
  • 6

2 Answers2

1

a few pointers, sum is a python builtin method for summing up lists of numbers so best not to use it as a variable name. also the syntax for adding to a variable is += but in your code you have =+. Your code works with just a change to that syntax ( i have also updated the variable name from sum to total and print only the total after the loop.

total = 0
for tag in tags:
    print(tag.contents[0])
    ChangeToInt = int(tag.contents[0])
    total += ChangeToInt
print(total)

Alternatively you could write this using pythons sum method and a list comprehension to generate the numbers.

total = sum([int(tag.contents[0]) for tag in tags])
print(total)

additionally you can check this question for the differnece between += and =+

Chris Doyle
  • 10,703
  • 2
  • 23
  • 42
  • Yes I got the answer, but it comes likes this 2418 2439 2458 2476 2494 2508 2520 2532 2541 2548 2551 2553 I wan't just single sum 2553. – Sixcheviexx Oct 27 '19 at 19:28
  • because in your code you are having your print statement inside the for loop so it will print every time it adds anything to the total. you should remove the print statement from inside the for loop and only print the total after the for loop – Chris Doyle Oct 27 '19 at 19:31
0

You simply have your increment syntax wrong:

sum =+ ChangeToInt

should instead be:

sum += ChangeToInt

Your code worked just fine for me after I fixed that.

Joseph Rajchwald
  • 487
  • 5
  • 13
  • Yes I got the answer, but it comes likes this 2418 2439 2458 2476 2494 2508 2520 2532 2541 2548 2551 2553 I wan't just single sum 2553. – Sixcheviexx Oct 27 '19 at 19:30