Your real problem here is that you're thinking out of order. You read the problem, and then started writing a solution before you really understood what it was asking for. That's actually really common to do when you see a big blob of text, and it takes practice and skill to go from there to a correctly working program.
Let's take this step by step:
Take in two numbers and a letter. If the letter is "a", the numbers will be added, if "s", subtracted, if "m", multiplied, and if "d", divided.
Here's the assignment. Rather than leave it in this form, let's convert it to a form that's easier to think about piece by piece:
- Take in two numbers and a letter
- if the letter is 'a', the numbers will be added
- if the letter is 's', subtracted
- if 'm', multiplied
- and if 'd', divided
You'll notice that I didn't change any of the text here, I just shifted the formatting around. If you read the two, you'll see that there's no difference between them.
Now we can think about turning that into code, because it's layed out like code. You may have heard the term "pseudocode", which means "something that is code-like". We could even do that here.
# Take in two numbers and a letter
num_one, num_two, letter = get_two_numbers_and_letter()
if letter is a: # if the letter is 'a'
print(num_one+num_two) # the numbers will be added
if letter is s: # if the letter is 's'
print(num_one-num_two) # the numbers will be subtracted
if letter is m: # if 'm'
print(num_one*num_two) # multiplied
if letter is d: # and if "d", divided
print(num_one/num_two)
You'll notice that this already looks an awful lot like Python. We can clean it up a little:
# take in two numbers
num_one = int(input('First number: '))
num_two = int(input('Second number: '))
# and a letter
letter = input('(a)dd, (s)ubtract, (m)ultiply, or (d)ivide? ')
# if the letter is 'a', the numbers will be added
if letter == 'a':
print(num_one + num_two)
# if the letter is 's', subtracted
if letter == 's':
print(num_one - num_two)
# if 'm', multiplied
if letter == 'm':
print(num_one * num_two)
# and if 'd', divided
if letter == 'd':
print(num_one / num_two)
Of course now the comments are a bit superfluous - all they describe is exactly what the code is doing. We're also using if
instead of elif
, which is a bit ambiguous and less efficient (not that that really matters at this point, but it's still true). So let's clean up the code just a little bit more:
num_one = int(input('First number: '))
num_two = int(input('Second number: '))
letter = input('(a)dd, (s)ubtract, (m)ultiply, or (d)ivide? ')
if letter == 'a':
print(num_one+num_two)
elif letter == 's':
print(num_one-num_two)
elif letter == 'm':
print(num_one*num_two)
elif letter == 'd':
print(num_one/num_two)
else:
print("I'm sorry, I don't recognize letter", repr(letter))
Bonus - On Debugging
If your professor has not been teaching you about the REPL then they're doing you a disservice. The REPL is great, and especially for beginners. You can experiment and get feedback immediately. Let's consider part of your original code:
num1 = int(input("please enter the first number"))
num2 = int(input("please enter the second number"))
lettler = input("please enter the operation")
a = num1+num2
if lettler + str(a):
print(num1 + num2)
You could just copy and paste this into the REPL, or even better, skip out on the input part, and just assign the values you want:
>>> num1 = 3
>>> num2 = 5
>>> lettler = 'a'
>>> a = num1 + num2
>>> if lettler + str(a):
... print(num1 + num2)
...
8
Great! We got what we expected! But... that's not actually how you verify an experiment. You have to design something that should fail. So what if we try lettler = 's'
?
>>> if lettler + str(a):
... print(num1+num2)
...
8
Huh. Well, that's not right. We should have just got nothing. Luckily, this is pretty simple, it's just an if
statement and a call to the print
function. We know it's not the print
that's the problem, our code never should have made it to that point. So let's check on what's in the if
statement:
>>> lettler + str(a)
's8'
Oh. That's a string. Hm. It's not an empty string, and it's true. Maybe you remember this from class, but maybe you don't. So let's go to google and type in 'python why is my non-empty string true?'
When I do that the first result I get is https://stackoverflow.com/a/9573259/344286 Which does a pretty good job of explaining it, and better yet, it even links us to the official documentation that tells us:
By default, an object is considered true...
Here are most of the built-in objects considered false:
...
empty sequences and collections: ''
...
Ahh! So because our string is not empty ('s8'
) it's going to evaluate to True
! Well, okay. So what do we actually want to do instead? We probably don't want to be adding strings together. Can we compare things instead? What if we try ==
?
>>> lettler = 'a'
>>> lettler == str(a)
False
Hm. Well, that's not what we expected, was it? I guess let's see what the values are:
>>> lettler
'a'
>>> a
8
Oh. Well yeah, those aren't equal! And a
isn't the letter 'a'
. Oh that's right! We said that a = num1 + num2
, so of course it's not a letter. Maybe in the future we shouldn't use letters for variable names. If we take a step back and think about what we want (if the letter is "a" the numbers should be added
) then we have the answer. We want to compare lettler
to "a"
:
>>> if lettler == 'a':
... print(num1 + num2)
...
8
>>> lettler = 's'
>>> if lettler == 'a':
... print(num1 + num2)
...
>>>
Ah. Perfect!