Question
I have attached my code here which adds the number from the text file avoiding the non-digits. For instance, 3.14159 in the file would be
I have attached my code here which adds the number from the text file avoiding the non-digits. For instance, "3.14159" in the file would be counted as two numbers to add to the total: a 3 and 14159; "21,756.54" would add three numbers to the total: 21, 756, and 54. but the code that I wrote is adding single numbers like in my text file the numbers are: 21.30 its not adding the 21 + 30 its doing the 2+1+3+0 can you fix that for me.
#file : countFile.py #date : 3/16/2021
def main(): print() print("Program to count the total") print("of the numbers in a file.") print("You will be asked to enter") print("the name of a file.") print("Written by Dhruvil Patel") print()
filename = input("Enter the name of a file to count: ") read = open(filename, 'r')
adding = 0 for line in read: digits = 0 for char in line: if char.isdigit(): digits = digits + int(char) else: adding = adding + digits digits = 0 if digits != 0: adding = adding + digits
print("The original file") print(filename) print("has been counted and the sum of") print("all numbers is", adding)
main()
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started