It looks like you made duplicate functions containing the same code, making it difficult to understand what block should be running to accomplish the desired output of the required task. Please try to clean up your code so that it is clear what your code is doing and meeting the task specifications.
#Compulsary Task1 #main.py: #function to find a op b and return result #main.py: #function to find a op b and return result def evaluate(a, b, op): #finds result based on op if(op == '*'): return a*b; elif op == '-': return a-b; else: return a+b; #function to validate input operator def getOp(): while True: op = input('\nEnter operator[+,-,*]: ')[0] #checks if operator is * or + or - if (op != '*' and op != '+' and op != '-'): print("Not an operator! Please enter an operator within.") continue else: break return op #function to validate input number def getNum(): while True: #catch exception if input number is not integer try: number = int(input('\nEnter Number: ')) except ValueError: print("Not an integer! Please enter an integer.") continue else: break # returns number after validation return number #function to validate file name def getFile(): while True: filename = input("\n Enter Filename or path : ") try: #append filename to the directory where the file is fname ="C:\\Users\\Dell\\Dropbox\\"+filename f = open(fname) except FileNotFoundError: print("Wrong file or file path") else: break #returns file pointer return f #to convert string to individual character list def split(word): return list(word) print("\n select a choice :\n 1. Input Equation\n 2. Input file name of the equations") choice = int(input("\n Enter Choice : ")) if(choice == 1): #path of output file path = r'output_new_file.txt' #opens output file in write mode f = open(path,'w') ch = 1 #gets user choice while(ch != '0'): #gets the input numbers number1 = getNum() number2 = getNum() #gets the operator op = getOp() #finds the result value result = evaluate(number1,number2,op) #creates the expression str_line = str(number1)+str(op)+str(number2)+'='+str(result); #displays output print(str_line) f.close() #opens file in append mode so that already written content not deleted f = open(path,'a') #writes expression f.write(str_line+"\n") #asks user choice to quit ch = input("\nIf u want to quit press 0 .....") #closes file f.close() elif choice == 2: #path of output file path = r"output_file.txt" #opens output file in write mode f = open(path,'w') #opens output file in write mode ip_file = getFile() data = ip_file.read().split("\n") for x in range(0,len(data)): #gets each char list --> 1+2 as 1, +, 2 x1 = split(data[x]) number1 = int(x1[0]) number2 = int(x1[2]) op = x1[1] #finds the result value result = evaluate(number1,number2,op) #creates the expression str_line = str(number1)+str(op)+str(number2)+'='+str(result); #displays output print(str_line) f.close() #opens file in append mode so that already written content not deleted f = open(path,'a') #writes expression f.write(str_line+"\n") f.close() ip_file.close() f.close()
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)