1

Can anyone help me finish this code please, I need to calculates the total of the numbers contained in the file and print the answer to the screen using the get_total function. I have the rest of the code running fine I just don't know how to add the numbers together and display the answer. I'm using python 3.3.2

def main():

     filename = input("Welcome, please enter file name:\t")
     menu()
     choice= int(input("Enter menu choice:\t"))

    while choice != 5:
         #get file choice from user
         if choice == 1:
             #create file
             create(filename)
         elif choice == 2:
             #read file
             read(filename)
         elif choice == 3:
             #append file
             append(filename)
         elif choice == 4:
             #get total
             get_total(filename)

         choice = int(input("Enter menu choice:\t"))

     print("\nApplication Complete")

 def menu():
     #user chooses a number from menu
     print("Choose a number to continue:\t\n\
     Select 1 to create a file\n\
     Select 2 to read a file\n\
     Select 3 to append to a file\n\
     Select 4 to calculate the total of a file\n\
     Select 5 to exit programme")

 def create(filename):

     #create file name
     outfile = open(filename,"w")
     again = "y"

      while again == "y":

         try:
             #user inputs integer
             num = int(input("Input number:\t"))
             outfile.write(str(num)+"\n")
             #asks user whether they want to continue or not
             again = input("Enter y to continue:\t")

         #if an error occurs
         except ValueError:
                       print("An error occured,please enter an integer:\t")

         except:
                       print("An undetermined error occurred")
      #close file
      outfile.close()

 def read(filename):

     print("\nReading File")

     try:
         #read integers entered onto file
         infile = open(filename,"r")

         for line in infile:

             number = int(line)
             print(number)

     except IOError:
             print("An error occured trying to read")
             print("the file", filename)

     except:
             print("An undefined error occurred")

 def append(filename):

     print("\nAppending to file")

     #user enters integers again
     outfile = open(filename, "a")
     again = "y"

     while again == "y":

         try:
             num = int(input("Input number to append to file:\t"))
             outfile.write(str(num)+"\n")
             again = input ("Enter y to continue:\t")

         except ValueError:
                 print("an error occured please an integer")
         except:
                 print("an undefined error occured")


         outfile.close()

 def get_total(filename):

     print("\nGetting total numbers contained in file")

     try:
         #user inputs integer
         num = int(input("Input number:\t"))
         outfile.write(str(num)+"\n")
         #asks user whether they want to continue or not
         again = input("Enter y to continue:\t")

     except IOError:
             print("An error occured trying to read")
             print("the file", filename)

     except:
             print("An undefined error occurred")          

 #call main
 main()

Thanks for your help

3 Answers 3

1
import re
def get_total(filename):

    print("\nGetting total numbers contained in file")
    num = int(input("Input number:\t"))
    f = open(fileName)
    found = 0
    for line in f:
        for matchNum in re.findall(r'[0-9]+',line):
        if matchNum == str(num):
            found = found + 1
    print("\nNo. of occurrences of num in file :: ",found       

Hope this solves your problem.

Sign up to request clarification or add additional context in comments.

Comments

1

This block of code will be helpful for your requirement:

def get_total(filename):
 total_sum = 0 
 print("Calculating total of the numbers contained in file")
 f = open(filename)
 try:
     #read inputs integer
     for line in f:  
         #split the string on whitespace, return a list of numbers
         # (as strings)
         numbers_str = line.split()
         #convert numbers to int
         numbers_int = [int(x) for x in numbers_str]

         total_sum=sum(numbers_int)

 except IOError:
         print("An error occured trying to read")
         print("the file", filename)

 except:
         print("An undefined error occurred")
 # prints out the total after the loop
 print("The total {}".format(total_sum))

Comments

0

You are going to need to create a variable to hold all the numbers you are reading in:

 def get_total(filename):
     total_sum = 0 # running total
     print("\nGetting total numbers contained in file")

     try:
         #user inputs integer
         num = int(input("Input number:\t"))
         outfile.write(str(num)+"\n")
         total_sum += num
         #asks user whether they want to continue or not
         again = input("Enter y to continue:\t")

     except IOError:
             print("An error occured trying to read")
             print("the file", filename)

     except:
             print("An undefined error occurred") 
     # prints out the total after the loop
     print("The total {}".format(total_sum))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.