Question

In: Computer Science

Python Language Only! Python Language Only! Python Language Only! Python 3 is used. When doing this...

Python Language Only!

Python Language Only!

Python Language Only!

Python 3 is used.

When doing this try to use a level one skill python, like as if you just learned this don't use anything advanced if you can please. If not then do it as you can.

Assume you have a file on your disk named floatnumbers.txt containing float numbers. Write a Python code that reads all the numbers in the file and display their average. Your code must handle any IOError (for example, file does not exist) and ValueError (for example, alphanumeric data found in the file such as letters) exceptions.

Solutions

Expert Solution

If you have any doubts, please ask in the comments, I will try to solve it as soon as possible. If you find my answer helpful, do UPVOTE.Thanks

The required Python code is given below:

#sum of numbers
sum = 0
#count of numbers
numbers = 0
#opening file
try:
with open("floatnumbers.txt","r") as file:
#reading each number
for line in file.readlines():
try:
#updating the sum
sum=sum+float(line)
#updating the number count
numbers=numbers+1
#handling the ValueError
except ValueError:
print("Oops cannot convert string to float")
#handling the IO error
except IOError:
print("File does not exist")
if numbers > 0:
#printing the average if count is greater than zero
print("Average: " + str(sum/numbers))

Code Snippet:

Output:

floatnumbers.txt

#sum of numbers sum = 0 #count of numbers numbers = 0 #opening file try: with open("floatnumbers.txt","r") as file: #reading each number for line in file.readlines(): try: #updating the sum sum=sum+float(line) #updating the number count numbers-number3+1 #handling the ValueError except ValueError: print ("Oops cannot convert string to float") #handling the Io error except IOError: print ("File does not exist") if numbers > 0: #printing the average if count is greater than zero print ("Average: " + str(sum/numbers))

Oops cannot convert string to float Average: 6.55


Related Solutions

Project #2 ## Language Should be Python. Using for, while, if, elif only. No advanced functions....
Project #2 ## Language Should be Python. Using for, while, if, elif only. No advanced functions. Assignment Specifications The program will compute and display information for a company which rents vehicles to its customers. For a specified customer, the program will compute and display the amount of money charged for that customer’s vehicle rental. The program should start by asking the user if he wants to continue. The answer is ‘Y’ for yes or ‘N’ for no. The basic structure...
Write a program in python programming language to implement/simulate a finite automaton that accepts (only): odd...
Write a program in python programming language to implement/simulate a finite automaton that accepts (only): odd Binary numbers // 00000000, 0101, 111111, etc. Show: Finite Automaton Definition, Graph, Table
Write a program in python programming language to implement/simulate a finite automaton that accepts (only): unsigned...
Write a program in python programming language to implement/simulate a finite automaton that accepts (only): unsigned integer numbers // 123, 007, 4567890, etc. Show: Finite Automaton Definition, Graph, Table.
PYTHON LANGUAGE PLEASE DO NUMBER 5 ONLY """ # 1. Based on Textbook R6.28 # Create...
PYTHON LANGUAGE PLEASE DO NUMBER 5 ONLY """ # 1. Based on Textbook R6.28 # Create a table of m rows and n cols and initialize with 0 m = 3 n = 4 # The long way: table = [] for row in range(m) : table.append([0]*n)    # The short way: # 2. write a function to print the table in row, column format, # then call the function ''' # using index def printTable(t): for i in range(len(t))...
Python language!!!!! №1 The translation from the Berland language into the Birland language is not an...
Python language!!!!! №1 The translation from the Berland language into the Birland language is not an easy task. Those languages are very similar: a berlandish word differs from a birlandish word with the same meaning a little: it is spelled (and pronounced) reversely. For example, a Berlandish word code corresponds to a Birlandish word edoc. However, it's easy to make a mistake during the «translation». Vasya translated word s from Berlandish into Birlandish as t. Help him: find out if...
Programming language in Python Suppose, for Jane, n1 = 3, n2 = 4, and n3 =...
Programming language in Python Suppose, for Jane, n1 = 3, n2 = 4, and n3 = 5. Also suppose, Jane iterates the number from 1 to 15. At the beginning, Jane sets count to 0, and then proceeds iterating the number from 1 to 15 and for each iteration does the following: for 1, count is increased by 1 because it is not divisible by 3, 4, and 5; count is now: 1 for 2, count is increased by 2...
Programming language is in python 3 For this project, you will import the json module. Write...
Programming language is in python 3 For this project, you will import the json module. Write a class named NobelData that reads a JSON file containing data on Nobel Prizes and allows the user to search that data. It just needs to read a local JSON file - it doesn't need to access the internet. Specifically, your class should have an init method that reads the file, and it should have a method named search_nobel that takes as parameters a...
Programming language is python 3 For this project, you will import the json module. Write a...
Programming language is python 3 For this project, you will import the json module. Write a class named NeighborhoodPets that has methods for adding a pet, deleting a pet, searching for the owner of a pet, saving data to a JSON file, loading data from a JSON file, and getting a set of all pet species. It will only be loading JSON files that it has previously created, so the internal organization of the data is up to you. The...
Using C language Problem!! <3 x 3 tic tac toe> 2 players are doing tic tac...
Using C language Problem!! <3 x 3 tic tac toe> 2 players are doing tic tac toe alternatively. Anyone could win when they first make a bingo for any line(horizontal, vertical, diagonal) [Constraints] 1. Use 2dimensional Array(3 X 3) 2. Each elements are 1 or 2 so that can show their player1, player2's mark. 3. Inputs are 1 through 9 as you can see 1 2 3 4 5 6 7 8 9 4. No inputs are duplicated 5. if...
When doing Machine Learning and Deep Learning (AI) research which language is better to use Java...
When doing Machine Learning and Deep Learning (AI) research which language is better to use Java or Python? When would you use Java and when would you use Python?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT