Home > Software design >  problem in using while loop in a calculator project
problem in using while loop in a calculator project

Time:03-22

I am creating a calculator and in that I have used a while loop so that the user can do the calculations again and again. but there is some problem in the while loop. this is the code:

print("CALCULATOR")

def addint(n1,n2):

    sum = n1   n2

    return sum

def minusint(n1,n2):

    minus = n1 - n2

    return minus

def mult(n1,n2):

    multsum = n1 * n2

    return multsum

def div(n1,n2):

    divsum = n1 / n2

    return divsum

n1 = int(input("Enter First Number:\n"))

n2 = int(input("Enter Second Number:\n"))

n3 = str(input("Which Operator?\n"))

while n1 <= 0:

    if n3 == ' ':

        print(f"The answer is {addint(n1,n2)}")

    elif n3 == '-':

        print(f"The answer is {minusint(n1,n2)}")

    elif n3 == '*':

        print(f"The answer is {mult(n1,n2)}")

    elif n3 == '/':

        print(f"The answer is {div(n1,n2)}")

    else:

        print("INVALID OPERATOR")

    break

and this is the output window:

CALCULATOR

Enter First Number:

20

Enter Second Number:

10

Which Operator?

-

the problem occurring is when I use the while loop after that in the output window the calculations are not happening as they are expected to happen. the output window just closes after i enter the numbers and the operator. and the second problem is that the user cannot do calculations again and again.

so please help me in this!

CodePudding user response:

  1. While loop runs the code inside as long as the condition is true. In this case, the condition is n1 <= 0. Therefore, if n1 = 20 the loop will not run even once. If you want an infinite loop, use while True.
  2. There is a break keyword at the end, so the loop will only run once.
  3. Your program will print the same result forever because the input instructions are outside of the loop.

Working example:

def addint(n1, n2):
    sum = n1   n2
    return sum

def minusint(n1, n2):
    minus = n1 - n2
    return minus

def mult(n1, n2):
    multsum = n1 * n2
    return multsum

def div(n1, n2):
    divsum = n1 / n2
    return divsum

while True:
    n1 = int(input("Enter First Number:\n"))
    n2 = int(input("Enter Second Number:\n"))
    n3 = input("Which Operator?\n")

    if n3 == ' ':
        print(f"The answer is {addint(n1,n2)}")
    elif n3 == '-':
        print(f"The answer is {minusint(n1,n2)}")
    elif n3 == '*':
        print(f"The answer is {mult(n1,n2)}")
    elif n3 == '/':
        print(f"The answer is {div(n1,n2)}")
    else:
        print("INVALID OPERATOR")

Notice that converting input("Which Operator?\n") to str is unnecesarry, because input function returns string.

CodePudding user response:

Why the while condition is "while n1 <= 0" ? Right off the box, you entered n1 = 20 which is NOT <=0. So, it doesn't enter the while loop at all.

CodePudding user response:

You can do like this

while True:
    n1 = int(input("Enter First Number:\n"))
    n2 = int(input("Enter Second Number:\n"))
    n3 = str(input("Which Operator?\n"))

    if n3 == ' ':
        print(f"The answer is {addint(n1,n2)}")

    elif n3 == '-':
        print(f"The answer is {minusint(n1,n2)}")

    elif n3 == '*':
        print(f"The answer is {mult(n1,n2)}")

    elif n3 == '/':
        print(f"The answer is {div(n1,n2)}")

    else:
        print("INVALID OPERATOR")
  • Related