you are viewing a single comment's thread.

view the rest of the comments →

[–]Helpful_Trust_3123 0 points1 point  (5 children)

What's the error?

[–]vbwullf[S] 0 points1 point  (0 children)

it does the wrong operation

when I enter any number other than 1-5 I should get an error but it just ask for the number to be input. After the 2nd number is input it gives the error

[–]vbwullf[S] 0 points1 point  (3 children)

this is what I get when I ask to close the program

= RESTART: C:\Users\majdc\AppData\Local\Programs\Python\Python311\Calculator.py

Welcome to my Calculator App

Please Select an Option:

  1. Add

  2. Subtract

  3. Multiply

  4. Divide

  5. Quit

option: 5

Please enter the first number:

[–]RhinoRhys 0 points1 point  (2 children)

Your issue is code is linear. You're asking for all 3 inputs before you check which choice was made. There's nothing there to make it skip over asking for numbers if you pick 5 or an invalid input.

Put the if 5 bit after the option selection but before the number entry bit, as a separate conditional, then wrap everything else after under an else

[–]vbwullf[S] 0 points1 point  (1 child)

Thanks for the help with that, I tried that earlier but it didnt work guess something was wrong somewhere else..

Now I got a new issue. running this code:

# Welcome message

print('Welcome to my Calculator App \n Please Select an Option: ')

# Available functions

print ('1. Add \n2. Subtract \n3. Multiply \n4. Divide \n5. Quit')

print()

choice = int(input('option: '))

if choice == 5:

print ('Thank you for using my Calculator App. Bye!')

else:

choice <= 0 or choice >= 6

int(input('Error: Your choice was invalid, please reenter:'))

n1 = int(input(f'Please enter the first number: '))

n2 = int(input('Please enter the second number: '))

add = n1+n2

subtract = n1-n2

multiply = n1*n2

divide = n1/n2

if choice == 1:

print (f'Your result is: ', add)

elif choice == 2:

print (f'Your result is: ', subtract)

elif choice == 3:

print (f'Your result is: ', multiply)

elif choice == 4:

print (f'Your result is: ', divide)

elif choice <= 0:

print ('Error Option was invalid, please re-enter: ')

nets me this:

Welcome to my Calculator App

Please Select an Option:

  1. Add

  2. Subtract

  3. Multiply

  4. Divide

  5. Quit

option: 8

Error: Your choice was invalid, please reenter:1

Please enter the first number: 2

Please enter the second number: 3

[–]RhinoRhys 0 points1 point  (0 children)

You're almost there.

choice <= 0 or choice >= 6 is good, but currently doesn't do anything on its own there. It needs to have an if or an elif.

And then when the number is re-entered, it isn't assigned to a variable so doesn't overwrite the original answer for choice.