Language: Python Question: How do you create a function that calculates the sum and average of the odd numbers that you read from the file?

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Language: Python

Question: How do you create a function that calculates the sum and average of the odd numbers that you read from the file?

(^) Python for Eve X
wex Eligible Expens X
Logout
I MasterClass | X
Bb CS170 FileFun X
Bb Syllabus of the X G anaconda pyth ×
as Average of od X
Əs Python progra X
blackboard.truman.edu/bbcswebdav/pid-1153421-dt-content-rid-14591826_1/courses/2021404103/CS170%20FileFun.pdf
A
CS170 FileFun
1 / 3
100%
Sum of odd integer numbers:
In this problem, you are going to read a series of integer values from the file and calculate the
sum and average of the odd numbers only.
You should create a function to complete this program as the following:
# complete the following function
def calculate_sum(filename):
try:
infile = open(filename, "r")
# calculate the sum
of the odd numbers only
except: # catch exceptions
print("")
# further, call the function with filename
calculate_sum("data.txt")
Format your program as the above and complete the body of the function that calculates the
sum and average of the odd numbers that you read from the file. For this program, you need
to create a text file named "data.txt" and place it in the same folder as the program file.
Input for this program are from a file named data.txt. Each line of the file contains one integer
number. For example, in the following file, your program will calculate the summation of the
numbers 33, 7,1 and 9. Hence, the summation would be 50.00 and average would be 12.50.
Sample Input (data.txt)
Sample Output
10
33
22
Sum of the odd numbers is: 50.00
7
Average is: 12.50
1
2
...
II
Transcribed Image Text:(^) Python for Eve X wex Eligible Expens X Logout I MasterClass | X Bb CS170 FileFun X Bb Syllabus of the X G anaconda pyth × as Average of od X Əs Python progra X blackboard.truman.edu/bbcswebdav/pid-1153421-dt-content-rid-14591826_1/courses/2021404103/CS170%20FileFun.pdf A CS170 FileFun 1 / 3 100% Sum of odd integer numbers: In this problem, you are going to read a series of integer values from the file and calculate the sum and average of the odd numbers only. You should create a function to complete this program as the following: # complete the following function def calculate_sum(filename): try: infile = open(filename, "r") # calculate the sum of the odd numbers only except: # catch exceptions print("") # further, call the function with filename calculate_sum("data.txt") Format your program as the above and complete the body of the function that calculates the sum and average of the odd numbers that you read from the file. For this program, you need to create a text file named "data.txt" and place it in the same folder as the program file. Input for this program are from a file named data.txt. Each line of the file contains one integer number. For example, in the following file, your program will calculate the summation of the numbers 33, 7,1 and 9. Hence, the summation would be 50.00 and average would be 12.50. Sample Input (data.txt) Sample Output 10 33 22 Sum of the odd numbers is: 50.00 7 Average is: 12.50 1 2 ... II
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 2 images

Blurred answer
Knowledge Booster
Concept of memory addresses in pointers
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education