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.
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))
Get Answers For Free
Most questions answered within 1 hours.