Question

hi can i please get the answer of this question? Write a python program that calculates...

hi can i please get the answer of this question?

Write a python program that calculates the average of numeric values in a string and ignores all other characters. Your program must print the sum and average on the screen. (Save your script as spotNumbers.py and spotNumbers.txt)

For example: If the input is: myString= "Biology = 66.0 Python = 90.0 Economics = 80.0 Statistics = 70.0"

The output wound be: Sum= 306.0 Average= 76.5

Hint: Consider using type build-in function in your code. Also, see documentation or exception handling in Python as it may prove to be handy in your code.

Homework Answers

Answer #1

myString = input()
total, count = 0, 0
for word in myString.split():
    word = word.strip()
    try:
        number = float(word)
        total += number
        count += 1
    except ValueError:
        pass

if count == 0:
    average = 0
else:
    average = total/count
print("Sum= " + str(total) + " Average= " + str(average))



Know the answer?
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
Similar Questions
Hi can i get answer of the given question please Pythonize is a new language, that...
Hi can i get answer of the given question please Pythonize is a new language, that is similar to English, however the first letter of each word in Pythonize is moved to the end. Furthermore, each word in this new language ends with ‘py’. Write a program that takes an English word as an input and prints the Pythonize equivalent on the screen. (Save your script as pythonize.py and pythonize.txt) For example: If the input is: "Python" The output would...
I can open the file in the program, but I cannot figure out how to properly...
I can open the file in the program, but I cannot figure out how to properly split the data and assign a grade to the number values in the text file. I keep getting :: ValueError: invalid literal for int() with base 10: 'Roger Jones 75\n' Below are the assignment details: This program processes grades for a small class. It reads an input file named grade_input.txt where each record contains a student’s first name, last name and numeric grade (a...
ASAP I need this code... Design and implement in Python language, a program that calculates the...
ASAP I need this code... Design and implement in Python language, a program that calculates the molecular weight of a formula. Your program should accept a formula in whatever form you choose as input from the user, and should display the molecular weight for that formula. Your program must be able to handle formulas that: consist of a single element, e.g., He    (worth 40 points) consist of a sequence of elements, e.g., H H O    (worth 20 points) contain numeric constants, e.g.,...
please write the code in java so it can run on jGRASP import java.util.Scanner; 2 import...
please write the code in java so it can run on jGRASP import java.util.Scanner; 2 import java.io.*; //This imports input and output (io) classes that we use 3 //to read and write to files. The * is the wildcard that will 4 //make all of the io classes available if I need them 5 //It saves me from having to import each io class separately. 6 /** 7 This program reads numbers from a file, calculates the 8 mean (average)...