Ethan
Ethan

Reputation: 23

Trying to break in a while loop for a calculator program while accepting int and str values

I am making a calculator program while I'm learning python, I learned Java in high school and don't know any advanced python functions yet, so try not to use anything too advanced, but lets get to my problem. Sorry if this question isnt organized properly I have never posted on here. the entire program will be at the bottom.

where I'm getting stuck is I have the user enter the operator that they want to use and then it asks for number 1 and number 2. If the user types 'exit' or 'stop' at anytime then the program exits, for the operator part it exits fine and displays the exit message. But when typing exit for num1 or num2 I don't get the response I want, i get many errors playing with it but mainly like num is int cant have string value, num is str cant have int, or it accepts 'exit'/'stop' but then goes onto num2 and then throws an error after anything is entered into num2

operator = (input("Would you like to add(+), subtract(-), multiply(*), divide(/) or use exponents(**)? "))

if operator.lower() == 'exit' or operator.lower() == 'stop':
    break                       #this part works fine and as intended

num1 = eval(input("Enter number 1: "))
if num1 == str() and (num1.lower() == 'exit' or num1.lower() == 'stop'):
    break                       #this part however doesnt work because i have int and str in the same variable

num2 = eval(input("Enter number 2: "))
if num2 == str() and (num2.lower() == 'exit' or num2.lower() == 'stop'):
    break

ive tried things from using float, str, or int for nums, messing around with the if statements positions, I want to be able to accept 'exit' or 'stop' as a value for num and then exit the while loop, which ends the program but ive tried for an hour or so and cant figure it out. so again my problem is i need the num1 and num2 values to be able to accept str and int and end the program when they are equal to 'stop' or 'exit'

operator = ''
num1 = ''
num2 = ''
while True:
    operator = (input("Would you like to add(+), subtract(-), multiply(*), divide(/) or use exponents(**)? "))

    if operator.lower() == 'exit' or operator.lower() == 'stop':
        break

    num1 = eval(input("Enter number 1: "))
    if num1 == str() and (num1.lower() == 'exit' or num1.lower() == 'stop'):
        break

    num2 = eval(input("Enter number 2: "))
    if num2 == str() and (num2.lower() == 'exit' or num2.lower() == 'stop'):
        break

    oldNum2 = num2

    if operator == 'add' or operator == '+':
        answer = num1 + num2
        print(num1, '+', num2, '=', answer)

    elif operator == 'subtract' or operator == '-':
        answer = num1 - num2
        print(num1, '-', num2, '=', answer)

    elif operator == 'multiply' or operator == '*':
        answer = num1 * num2
        print(num1, '*', num2, '=', answer)

    elif operator == 'divide' or operator == '/':
        answer = num1 / num2
        print(num1, '/', num2, '=', answer)

    elif operator == 'exponents' or operator == '**':
        answer = num1 ** num2
        print(num1, '**', num2, '=', answer)

    else:
        print('Please type a valid operator...')
print('Program has exited due to user input.')

Upvotes: 2

Views: 417

Answers (3)

Prune
Prune

Reputation: 77910

Your functional problem is at the start of those validation checks:

if num1 == str() and ...

You just failed the check, right there. I think you're trying to check the type of the input after the eval (which, by the way, is a baaaaaad function to use).

str() is a call to convert the empty argument to a string. This returns an empty string. Since this will not be equal to any non-empty input, you fail.

Instead, use the function supplied for this purpose:

if isinstance(num1, str)

Better yet, check the values before you try a conversion:

user_input = input("What would you like to do?")
if user_input in ("stop", "exit"):
    break

Upvotes: 0

Alexander Freyr
Alexander Freyr

Reputation: 918

There are probably many answers to your question, but what I would do is get rid of eval() and just treat the input() since it will always be in a string format.

num1 = input("Enter number 1: ")
if num1.lower() == 'exit' or num1.lower() == 'stop':
    break
    
num2 = input("Enter number 2: ")
if num2.lower() == 'exit' or num2.lower() == 'stop':
    break

But since num1 and num2 are both in a string format after the if statements, make sure to cast them to an integer (or float)

num1 = int(num1)
num2 = int(num2)

Upvotes: 0

Aziz Sonawalla
Aziz Sonawalla

Reputation: 2502

Basically just treat num1 and num2 as strings until you know they are not stop or exit and then parse the number out of them:

def isExitCommand(userInput):
  return userInput.lower() == 'exit' or userInput.lower() == 'stop'

while True:
  operator = str(input("Would you like to add(+), subtract(-), multiply(*), divide(/) or use exponents(**)? "))

  if isExitCommand(operator):
      break                       #this part works fine and as intended

  num1 = input("Enter number 1: ")
  if isExitCommand(num1):
      break
  else:
    num1 = float(num1)             

  num2 = input("Enter number 2: ")
  if isExitCommand(num2):
      break
  else:
    num2 = float(num2)

  print("You entered {} {} {}".format(num1, operator, num2))

Also there's no need to check if the input is a string, but if you do want to then num1 == str() is incorrect. str() just gives a blank string. You want to do isinstance(s, str) instead for Python 3.x

Upvotes: 0

Related Questions