Question

I need help to calculate the sum of this, i just don't know how...... file_name =...

I need help to calculate the sum of this, i just don't know how......

file_name = input("What is the value-probability file? ")


    with open(file_name , "r") as final:

        for numbers in final:

            split_function = numbers.split()

            num_one = float(split_function[0])

            num_two = float(split_function[1])

            final = num_one * num_two

            final_line = final

So when my code runs the output looks like this :

What is the value-probability file? hw3_part2_test1.txt
-0.567032
-17.630715000000002
3.194056
4.398823
13.990935

I need all these numbers now to add to the next line and next line and give me sum..

I need the output answer to be : 3.386066999999997

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#assuming each line has 2 numbers separated by space, and you want the sum of product of these 2 numbers from all the lines
#below code will store that sum in the variable final_line
file_name = input("What is the value-probability file? ")
final_line=0
with open(file_name , "r") as final:
for numbers in final:
split_function = numbers.split()
num_one = float(split_function[0])
num_two = float(split_function[1])
final = num_one * num_two
final_line = final_line + final
  
print(final_line)

Add a comment
Know the answer?
Add Answer to:
I need help to calculate the sum of this, i just don't know how...... file_name =...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Python function: This is my code and I don't know how to add the column to...

    Python function: This is my code and I don't know how to add the column to the csv file: def write_with_averages(read,write): with open (read,'r') as f_in: header = f_in.readline() reader=csv.reader(f_in) with open (write,'w') as f_out: writer=csv.writer(f_out) New_data=[] for Name, Test1, Test2, Test3 in reader: Total=(float(Test1)+float(Test2)+float(Test3)) average=Total/3 New_data.append(average) Hint: Skip the first line of the file as it is a header line Hint: All values come in as strings, so convert to floats before mathematical comparisons and calculations. File Format Name,Test1,Test2,Test3...

  • I need python help .. I need to know if a user were to input 3...

    I need python help .. I need to know if a user were to input 3 commands to a program and run it such as specify: Usage: ./filemaker INPUTCOMMANDFILE OUTPUTFILE RECORDCOUNT (./filemaker is the program)( (INPUTCOOMANDFUILE= a text file that contains key words that need to be searched through then decided what to do ) (RECORDCOUNT= number) Given an input file that contains the following: That specifies the text, then the output, and the number of times to be repeated...

  • Hi I was hoping to get some help with my code for this problem. I don't...

    Hi I was hoping to get some help with my code for this problem. I don't know how to correctly read a txt file and the difference between using string and cstring. My code is at the bottom please help thanks. 1. Create the following “data.txt” file. Each record consists of customer's name and their balance: Maria Brown 2100.90 Jeffrey Jackson 200.20 Bernard Smith 223.10 Matthew Davenport 1630.20 Kimberly Macias 19100.90 Amber Daniels 2400.40 The records in the file will...

  • When I run my program it gives me these exceptions and I don't know what it...

    When I run my program it gives me these exceptions and I don't know what it means by it : Exception in thread "main" java.lang.NumberFormatException: For input string: "Code" Does anyone know what I need to change for this to work?

  • I am having a hard time with my program to assignment 4.12. Here are the instructions:...

    I am having a hard time with my program to assignment 4.12. Here are the instructions: The Payroll Department keeps a list of employee information for each pay period in a text file. The format of each line of the file is the following: <last name> <hours worked> <hourly wage> Write a program that inputs a filename from the user and prints to the terminal a report of the wages paid to the employees for the given period. The report...

  • I need help writing this C code. Here is the description: Let's say we have a...

    I need help writing this C code. Here is the description: Let's say we have a program called smart typing which does two things: The first letter of a word is always input manually by a keystroke. When a sequence of n letters has been typed: c1c2...cn and the set of words in the dictionary that start with that sequence also have c1c2...cnc then the smart typing displays c automatically. we would like to know,  for each word in the dictionary,...

  • Compute sum of the four digits in your AccessID and print it out. Please initialize your...

    Compute sum of the four digits in your AccessID and print it out. Please initialize your access ID as input data by using “DC” syntax. Your output should be in this format: My AccessID is ab1234 The sum is 10 Paste your code and screenshot of the output. . . Please create a very, very simple program as I am only a beginner. You don't need to store the four digits as variables. This is what I have so far....

  • Could anyone help add to my python code? I now need to calculate the mean and...

    Could anyone help add to my python code? I now need to calculate the mean and median. In this programming assignment you are to extend the program you wrote for Number Stats to determine the median and mode of the numbers read from the file. You are to create a program called numstat2.py that reads a series of integer numbers from a file and determines and displays the following: The name of the file. The sum of the numbers. The...

  • Hello I need help with python programming here is my code # Trivia Challenge # Trivia...

    Hello I need help with python programming here is my code # Trivia Challenge # Trivia game that reads a plain text file import sys def open_file(file_name, mode): """Open a file.""" try: the_file = open(file_name, mode) except IOError as e: print("Unable to open the file", file_name, "Ending program.\n", e) input("\n\nPress the enter key to exit.") sys.exit() else: return the_file def next_line(the_file): """Return next line from the trivia file, formatted.""" line = the_file.readline() line = line.replace("/", "\n") return line def next_block(the_file):...

  • I need help with a java problem, I need the following method               ...

    I need help with a java problem, I need the following method                    Method that reads a file and creates a TreeMap, the key is the "salary" and the value is an object "Profesor"                    my txt file is like this:                        Harry smith   345677                        andy Connor   123456

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT