Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Im new to coding and am trying to make myself a calculator in pyton idle 3.10 64 bit and it isnt working

I’ve written this code so far:

OPR = input('''
which functions do you wish to use?
a for arithmetic
g for graphic
    ''')

CON = "yes"
while CON == "yes" and OPR == "a":

    OPR = input('''
    Please type in the math operation you would like to complete:
    + for addition
    - for subtraction
    * for multiplication
    / for division
    ^ for exponent
    r for radical
    a for average
    ''')

    NU1 = int(input("First number?"))
    NU2 = int(input("Second number?"))

    ADD = NU1+NU2
    SUB = NU1-NU2
    MUL = NU1*NU2
    DIV = NU1/NU2
    EXP = NU1**NU2
    RAD = NU1**(1/NU2)
    AVG = 0.5*(NU1+NU2)
    
    if OPR == "+":
        print(str(NU1) + "+" + str(NU2) + "=" + str(ADD))
        ITR = ITR + 1
    elif OPR == "+":
        print(str(NU1) + "-" + str(NU2) + "=" + str(SUB))
        ITR = ITR + 1
    elif OPR == "+":
        print(str(NU1) + "*" + str(NU2) + "=" + str(MUL))
        ITR = ITR + 1
    elif OPR == "+":
        print(str(NU1) + "/" + str(NU2) + "=" + str(DIV))
        ITR = ITR + 1
    elif OPR == "+":
        print(str(NU1) + "^" + str(NU2) + "=" + str(EXP))
        ITR = ITR + 1
    elif OPR == "+":
        print(str(NU1) + "^(1/" + str(NU2) + ")=" + str(RAD))
        ITR = ITR + 1
    elif OPR == "+":
        print(str(NU1) + " and " + str(NU2) + " average to " + str(AVG))
        ITR = ITR + 1 

I’m yet to add the graphic function or make use of the ITR variable but I think what I have so far should work. So many things go wrong depending on what I do, are there any massive errors I’m making?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

I’ve reorganized your code so that it works the way I think you are expecting. I don’t do anything with the first question’s answer; that’s an exercise left for the reader.

opr = input('''
which functions do you wish to use?
a for arithmetic
g for graphic
    ''')

iteration = 0
while True:

    opr = input('''
    Please type in the math operation you would like to complete:
    + for addition
    - for subtraction
    * for multiplication
    / for division
    ^ for exponent
    r for radical
    a for average
    q to quit
    ''')

    if opr == 'q':
        break

    num1 = int(input("First number? "))
    num2 = int(input("Second number? "))

    if opr == "+":
        answer = num1+num2
        print(num1,  "+" , num2, "=", answer)
    elif opr == "-":
        answer = num1-num2
        print(num1,  "-" , num2, "=", answer)
    elif opr == "*":
        answer = num1*num2
        print(num1,  "*" , num2, "=", answer)
    elif opr == "/":
        answer = num1/num2
        print(num1,  "/" , num2, "=", answer)
    elif opr == "^":
        answer = num1**num2
        print(num1,  "^" , num2, "=", answer)
    elif opr == "r":
        answer = num1**(1/num2)
        print(num1,  "^(1/" , num2, ")=", answer)
    elif opr == "a":
        answer = 0.5*(num1+num2)
        print(num1,  " and " , num2, " average to ", answer)

    iteration += 1
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading