Click here to Skip to main content
15,917,455 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi everyone now I have a problem where I want to add an option in my software where it multiplies the integers and floats on each line in my text file and adds them all up together and outputs the answer when the user accesses this option.But I'm getting something else, can someone help? My text file looks like this:
```Jumpst, comedy, 89, 6.0
Endgame, science-fiction, 58, 4.5
Up, animated, 42, 3.0
IT, horror, 50, 6.2
Johnwick, action, 10, 5.9
Palmsprings, romance, 33,End 8.0
Parasite, thriller, 12, 0.75
Harrypotter, fantasy, 60, 6.0
Babydriver, music, 22, 4.3
1917, war, 66, 1.75```

And my code is:

Python
def totalvalue():
    total = open('A3_s3899885_stock.txt', 'r')
    content = total.readlines()
    a = 0
    for line in content:
        for i in line:
            if i.isdigit() == True:
                a += int(i)
                print("The sum is:", a)

I think i need a dictionary but I dont know how to do it.

What I have tried:

Python
def totalvalue():
    total = open('A3_s3899885_stock.txt', 'r')
    content = total.readlines()
    a = 0
    for line in content:
        for i in line:
            if i.isdigit() == True:
                a += int(i)
                print("The sum is:", a)
Posted
Updated 4-Jun-21 1:08am
v2
Comments
Patrice T 4-Jun-21 7:14am    
"getting something else"
What is the something else ?
and what result do you expect ?

Not like that!
Look at your data: it's what is called a CSV (or Comma Separated Values) file, and that means that processing it character by character as you are won't work: you just build up something that is nothing like the actual data.
For example, with your code the first line of your file will produce the output:
The sum is:8
The sum is:17
The sum is:23
The sum is:23
Where at a guess it should produce just this:
The sum is:534.0

You need to do two things:
1) Simple: print the result after each whole line has been processed.
2) Read each line as CSV data, and "split it up" on the commas to separate the fields.

This may help: csv — CSV File Reading and Writing — Python 3.9.5 documentation[^]
 
Share this answer
 
Comments
Sai nathi 4-Jun-21 7:03am    
sorry i'm new to python and still dont understand how i can multiply and add each line to get the output, can you provde some sample code please!:)
Assuming that 'End' in
Quote:
Palmsprings, romance, 33,End 8.0
is just a typo, you could write a code similar to
Python
s = '''Jumpst, comedy, 89, 6.0
Endgame, science-fiction, 58, 4.5
Up, animated, 42, 3.0
IT, horror, 50, 6.2
Johnwick, action, 10, 5.9
Palmsprings, romance, 33, 8.0
Parasite, thriller, 12, 0.75
Harrypotter, fantasy, 60, 6.0
Babydriver, music, 22, 4.3
1917, war, 66, 1.75'''

sum = 0.0
for line in s.splitlines():
  f = line.split(',')
  sum = sum + float(f[2]) + float(f[3])

print(sum)
 
Share this answer
 
Comments
Sai nathi 4-Jun-21 7:20am    
but all he information is in a text file and you are saying to make it a variable. Do you know how i can multiply the '89 and 6.0' and add them with the next line from the text file?
CPallini 4-Jun-21 7:24am    
def totalvalue():
    total = open('A3_s3899885_stock.txt', 'r')
    content = total.readlines()
    sum = 0.0
    for line in content:
        f = line.split(',')
        sum = sum + float(f[2]) + float(f[3])

    print("The sum is:", sum)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900