0

program is at the bottom

In my program im trying to make a working calculator, i have the loops down, entering valid operators, different operators, and now im trying to allow previous answers to be used.

lets say you use +, addition

num1 = 5

num2 = 5

then print (num1+num2)...etc

now the answer is 10 so i assign prevAnswer to answer and now they are both 10

one of my if loops is the following:

    if num2 == 0:
    prevAnswer = num1
    num1 = num2
    num2 = prevAnswer

I initialized num2 as an int and the value of 0, so if the user doesn't enter anything it should be 0 right? then I make the num1 = the previous answer and num2 = num1, swapping variables so if i were to have the previous value of 15 then I wouldn't have 5 - 15 = -10 as that wouldn't make sense in a calculator.

I've ran my program in debugger and at the time of (value error invalid literal for int() with base 10: '') this is where it says the error is:

num2 = int(input("Enter number 2: "))

in debugger at this error these are my variables, answer is int 8, num1 is int 2, num2 is int 3, operator is string +, prevAnswer is int 8, ive tried over loading my code with int() so this would work and nothing i seem to try changes anything it just gives me value errors or name errors

please try to ignore the mess of ints and everything i know there is definitely things here that i dont need and i will learn to use define functions to make everything better looking for others and easier for myself. i just have no idea where ive gone wrong ive searched up all the errors i got and i cant use any of the functions or solutions they use it just gives me other errors. finally, sorry about the indentation, this isnt what my program looks like and i assume it wont run unless you auto indent.

def isExitCommand(userInput):
return (userInput == 'exit' or userInput == 'stop' or userInput == 'Exit' or userInput == 'EXIT' or 
userInput ==
        'Stop' or userInput == 'STOP')


def calc_exponent(num, pow):
result = 1
for i in range(pow):
    result = result * num
return result


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

if isExitCommand(operator):
    break

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

num2 = int(input("Enter number 2: "))
if num2 == 0:
    prevAnswer = num1
    num1 = num2
    num2 = prevAnswer

if isExitCommand(num2):
    break

else:
    num2 = int(num2)

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

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

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

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

    elif operator == 'exponents' or operator == '**':
        num1 = int(num1)
        num2 = int(num2)
        answer = calc_exponent(num1, num2)
        print(num1, '**', num2, '=', answer)
    else:
        print('Please type a valid operator...')

prevAnswer = answer

print('Program has exited due to user input.')

1 Answer 1

1

If the user doesn't enter anything, the string value is "" (empty) which has an error when converting to int.

A quick solution is to replace the empty input with 0:

num1 = int(input("Enter number 1: ") or 0)  # use 0 if empty input

Output

Would you like to add(+), subtract(-), multiply(*), divide(/) or use exponents(**)? +
Enter number 1:
Enter number 2: 34
0 + 34 = 34
Sign up to request clarification or add additional context in comments.

2 Comments

Hey, thanks this helped me with my problem :) but quick question, why didnt instantiating the variable with a value of 0 and then checking if num2 was still 0 because nothing was entered work?
Something is always entered, even it's an empty string

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.