vendredi 25 septembre 2020

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.')

Aucun commentaire:

Enregistrer un commentaire