Click here to Skip to main content
15,881,767 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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.


What I have tried:

#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()    
Posted
Updated 17-Aug-22 0:38am
Comments
Richard Deeming 17-Aug-22 6:22am    
Those instructions from your teacher are what you need to do. They are not a question for anyone else to answer.
Richard MacCutchan 17-Aug-22 6:28am    
Choice 2 is supposed to read the input from the file. In your code it does the same as choice 1.

1 solution

What you have posted is what your teacher didn't like about the code you submitted, and is giving you a chance to correct the problems - which are described pretty well.

Since you wrote the code, you know how it works so it should be fairly easy for you to rework is as required.

If you didn't, then you should throw it away and write your own - we aren't here to do your homework for you and won't tidy that code so you get a passing grade.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900