-3

I have the following code:

print('''How to use this calculator:
1 - Enter the type of function you want it to do like addition(+), subtraction(-), etc.
2 - Enter your number and it'll keep asking you until you say it to stop
3 - Voila! you have your answer\n''')
            
while 1 > 0:
    function = input("Enter the type of function you want to do:\n")
    if function == 'addition' or 'Addition' or 'ADDITION' or 'aDDITION' or '+' or 'add':
        num1 = int(input("Enter your number:\n"))
        num2 = int(input("Enter your number:\n"))
        total = num1 + num2
        print('Your answer is ' + str(total))
        continue
    elif function == 'subtraction' or 'Subtraction' or 'SUBTRACTION' or 'sUBTRACTION' or '-' or 'subtract':
        num1 = int(input("Enter the number you want to subtract from:\n"))
        num2 = int(input(f"Enter the number you want to subtract from {num1}:\n"))
        total = num1 - num2
        print('Your answer is' + str(total))
        continue
    elif function == 'multiplication' or 'multiply' or '*' or 'Multiply' or 'MULTIPLY':
        num1 = int(input("Enter the number you want to multiply:\n"))
        num2 = int(input(f"Enter the number you want to multiply {num1} wit:\n"))
        total = num1 * num2
        print('Your answer is' + str(total))
        continue
    elif function == 'divide' or 'DIVIDE' or '/':
        num1 = int(input("Enter the number you want to divide:\n"))
        num2 = int(input(f"Enter the number you want to divisor for {num1}:\n"))
        total = num1 / num2
        print('Your answer is' + str(total))
        continue
    elif function == 'stop' or 'STOP' or 'Stop' or 'sTOP':
        break
    else:
        print('Can\'t understant what you want to do please write your choice in the format below\n' + '''How to use this calculator:
1 - Enter the type of function you want it to do like addition(+), subtraction(-), etc.
2 - Enter your number and it'll keep asking you until you say it to stop
3 - Voila! you have your answer''')

I have a infinite loop which asks you to enter a function. And I have if-elif-else statements to see, which function the user inputed. I want the function the user inputed to be the one that gets activated, but for a reason it always does addition. Any help is appreciated!

CozyCode
  • 484
  • 4
  • 13

1 Answers1

0

In python a string can get evaluated as a boolean. An empty string returns False and a non-empty one returns True.

Example:

print(bool("A non empty string"))

Output:

True

Example:

print(bool(""))  # <---- Empty String

Output:

False

In the if statements you have, you wrote:

if function == 'addition' or 'Addition' or 'ADDITION' or 'aDDITION' or '+' or 'add':
...

Python first checks if function equals "addition" then if not it continues to the next condition and that is simply "Addition". Since there is no comparison or anything like that it simply gets evaluated to True and, thus the if statement becomes True, because you used or (So only one of the conditions have to be True.)

To fix this you have to add function == ... to your every check as such:

if function == 'addition' or function == 'Addition' or function == 'ADDITION' or function == 'aDDITION' or function == '+' or function == 'add':
    ...

To make this more readable you can use the in keyword and check if function is in the tuple as such:

if function in ('addition', 'Addition', 'ADDITION', 'aDDITION', '+', 'add'):
...

And to make this even better you can upper case the function and check if function is ADDITION only not every combination like "Addition", "aDdition"...

This is how you do it:

if function.upper() in ('ADDITION', '+', 'ADD'):
    ...

And here is the full working code (I also did a little bit of cleaning):

print('''How to use this calculator:
1 - Enter the type of function you want it to do like addition(+), subtraction(-), etc.
2 - Enter your number and it'll keep asking you until you say it to stop
3 - Voila! you have your answer\n''')
            
while True:
    function = input("Enter the type of function you want to do:\n").upper()

    if function in ('ADDITION', '+', 'ADD'):
        num1 = int(input("Enter your number:\n"))
        num2 = int(input("Enter your number:\n"))
        total = num1 + num2
        print(f'Your answer is {total}')
        continue

    elif function in ('SUBTRACTION', '-', 'SUBTRACT'):
        num1 = int(input("Enter the number you want to subtract from:\n"))
        num2 = int(input(f"Enter the number you want to subtract from {num1}:\n"))
        total = num1 - num2
        print(f'Your answer is {total}')
        continue

    elif function in ('MULTIPLICATION', '*', 'MULTIPLY'):
        num1 = int(input("Enter the number you want to multiply:\n"))
        num2 = int(input(f"Enter the number you want to multiply {num1} wit:\n"))
        total = num1 * num2
        print(f'Your answer is {total}')
        continue

    elif function in ('DIVISION', '/', 'DIVIDE'):
        num1 = int(input("Enter the number you want to divide:\n"))
        num2 = int(input(f"Enter the number you want to divisor for {num1}:\n"))
        total = num1 / num2
        print(f'Your answer is {total}')
        continue

    elif function == 'STOP':
        break

    else:
        print('Can\'t understand what you want to do please write your choice in the format below\n' + '''How to use this calculator:
1 - Enter the type of function you want it to do like addition(+), subtraction(-), etc.
2 - Enter your number and it'll keep asking you until you say it to stop
3 - Voila! you have your answer''')
CozyCode
  • 484
  • 4
  • 13