-1
# Pick the numbers you'd like to use
number1 = (input('What is the first number?\n'))
number2 = (input('What is the second number?\n'))
# Choose what to do with those numbers
Choice = input('Would you like to add, subtract, multiply or divide?\n ')
# Where the calculating happens, if it doesn't go through the error message asking you to use numbers will happen
try :
  # Converting the numbers from str into float, if this doesnt happen it means numbers were not used
  num1 = float(number1)
  num2 = float(number2)
  if Choice is add :
    answer = num1 + num2
    elif Choice is subtract : 
      answer = num1 - num2
        elif Choice is divide :
          answer = num1/num2
            elif Choice is multiply
              answer = num1 * num2
print(answer)

# The error message if the numbers you gave werent in numeric form
except :
  print('Please choose proper numbers in numeric form instead of letter/words')

This is the code, the issue I get is:

'File "main.py", line 13 elif Choice is subtract : ^ SyntaxError: invalid syntax'

Any help would be appreciated, thanks :). (If this code just wouldn't work at all, please lmk. Im currently going through a book on how to code and thought this would be fun to try since I just learned about booleans and variables)

  • Alright so I fixed the issue with the elif not being lined up, but now line 19 with print(answer) apparently has an invalid syntax – Thyme Travler Oct 03 '21 at 21:46
  • anddd I fixed that but now when you get to the choice between adding, subtracting, dividing and multiplying it just goes to the error message :/ – Thyme Travler Oct 03 '21 at 21:47

1 Answers1

1

I think you mean to do this:

if Choice == 'add':
    answer = num1 + num2
elif Choice == 'subtract': 
    answer = num1 - num2
elif Choice == 'divide':
    answer = num1/num2
elif Choice == 'multiply':
    answer = num1 * num2

Be careful with indentation. If you have a single if / elseif / elseif / elseif / else chain, each condition should be at the same indent level, and each body should match its condition plus one indent (typically 4 spaces or 1 tab).

To compare a string captured from the user with input() to a literal string, you would do:

if Choice == 'add':

You need to specify the quotation marks around 'add', otherwise it would try to reference a variable called add, which isn't defined.

For == vs is when checking the string, see Why does comparing strings using either '==' or 'is' sometimes produce a different result?

axby
  • 303
  • 3
  • 9
  • ohh thanks, what is "is" used for then? – Thyme Travler Oct 03 '21 at 21:47
  • @ThymeTravler I updated the answer, see https://stackoverflow.com/questions/1504717/why-does-comparing-strings-using-either-or-is-sometimes-produce-a-differe . My understanding is that "is" checks if it is actually the same instance of the string, but "==" checks if they have the same value. – axby Oct 03 '21 at 21:51