-9

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.

This is what I have:

num1 = int(input("please enter the first number"))
num2 = int(input("please enter the second number"))
lettler = input("please enter the operation")

a = int(num1 + num2)
s = int(num1 - num2)
m = int(num1 * num2)
d = int(num1 / num2)

if lettler + str(a):
    print(num1 + num2)
else:
    if lettler + str(s):
        print(num1 - num2)
    else:
        if lettler + str(m):
            print(num1 * num2)
        else:
            if lettler + str(d):
                print(float(num1) / num2)

But my professor is telling me it is wrong. What can I do to fix it?

Wayne Werner
  • 49,299
  • 29
  • 200
  • 290
  • 6
    You shouldn't need your professor to tell you this is wrong; simply running it would have done so. – Daniel Roseman Oct 05 '17 at 12:49
  • how can i fix it, do I hv to use "elif" statement. – Wilnny Abreu Oct 05 '17 at 12:50
  • Fix it one line at a time and re-run to check if errors are going. Start with indentation. – Sam Chats Oct 05 '17 at 12:50
  • num1 = int(input("please enter the first number")) num2 = int(input("please enter the first number")) operation = str(input("please enter the operation")) a = int(num1 + num2) s = int(num1 - num2) m = int(num1 * num2) d = int(num1 / num2) if operation + str(a): print(num1 + num2) elif operation - str(s): print(num1 - num2) – Wilnny Abreu Oct 05 '17 at 12:52
  • why just add the number, why is not subtracting – Wilnny Abreu Oct 05 '17 at 12:52
  • 1
    What exactly are your `if`s checking? – Xiobiq Oct 05 '17 at 12:52
  • 2
    What do you expect the `lettler + str(a)` in your `if` to become? – Klaus D. Oct 05 '17 at 12:53
  • when I type s I want to subtract, but is just working with the a – Wilnny Abreu Oct 05 '17 at 12:53
  • a should become num1 + num2? – Wilnny Abreu Oct 05 '17 at 12:54
  • 3
    @WilnnyAbreu: What do you expect `lettler + str(a)` to produce? Under what circumstances would it ever be false? If you were to *speak aloud* the condition you want to check in your `if` statements, what would that description be? Do your `if` statements currently check that condition? – David Oct 05 '17 at 12:55
  • letter + str(A) if is false should ask you for the next letter – Wilnny Abreu Oct 05 '17 at 12:57
  • If the letter is "a", the numbers will be added, if "s", subtracted, if "m", multiplied, and if "d", divided. – Wilnny Abreu Oct 05 '17 at 12:57
  • @WilnnyAbreu: Yes, that's correct. Now, under what circumstances *would it* be false? – David Oct 05 '17 at 12:57
  • @WilnnyAbreu: `"if the letter is 'a'"` - That's a good start. Your `if` statements, however, are *not* checking that condition. The condition you want to check is: "if letter equals 'a'". How would you express that in code? – David Oct 05 '17 at 12:58
  • We understand what you want your code to do. We are trying to help you understand why it's wrong. Klaus and David are hinting that `lettler + str(a)` will **never** be False. – PM 2Ring Oct 05 '17 at 12:58
  • @davi if one letter is false should ask you for the next letter? – Wilnny Abreu Oct 05 '17 at 12:59
  • letter == str(A)? – Wilnny Abreu Oct 05 '17 at 12:59
  • That's getting closer. What you want is `if letter == 'a':` and `elif letter == 's':` Etc. – PM 2Ring Oct 05 '17 at 13:01
  • 2
    @WilnnyAbreu: Why? What would `str(a)` produce? Put that exact operation in your code and output the result, observe what it produces. Ask yourself *why* it would do that. Ask yourself what the `a` variable is and where it comes from. You have to think logically about this, you can't just type code-like things and expect the system to know what you mean. Ask yourself, what is the difference between the variable `a` and the string `"a"`? – David Oct 05 '17 at 13:01
  • 4
    Side note, unrelated to the specific problem being faced... Currently your code produces all possible calculations and *then* attempts to determine which calculation to output. This will fail spectacularly if anybody ever tries to add a zero to another number, an operation which *shouldn't* fail. Don't perform the calculation until you determine what calculation the user asked you to perform. – David Oct 05 '17 at 13:05

3 Answers3

6

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!

Wayne Werner
  • 49,299
  • 29
  • 200
  • 290
1

An if statement works by testing if the expression you put after the if keyword is true. What you’re doing is adding two strings. In python, a string with characters will always come out to true, thus, the first if is always executed. What I have done is modify your conditions to match what you want:

num1 = int(input("please enter the first number: "))
num2 = int(input("please enter the second number: "))
lettler = input("please enter the operation: ")

if lettler == 'a':
    print(int(num1 + num2))
elif lettler == 's':
    print(int(num1 - num2))
elif lettler == 'm':
    print(int(num1 * num2))
elif lettler == 'd':
    try:
        print(float(num1 / num2))
    except ZeroDivisionError:
        print('You tried to divide by zero')

See how I test if lettler (your operation input) is equal to the corresponding letter? That’s what you want. I also added a try/except that makes sure your program doesn’t crash if you divide by zero.

Steampunkery
  • 3,839
  • 2
  • 19
  • 28
0
num1 = int(input("please enter the first number"))
num2 = int(input("please enter the second number"))
lettler = input("please enter the operation")

a = int(num1 + num2)
s = int(num1 - num2)
m = int(num1 * num2)
d = int(num1 / num2)

if lettler == "a":
    print(num1 + num2)
elif lettler == "s":
    print(num1 - num2)
elif lettler == "m":
    print(num1 * num2)
elif lettler == "d":
    print(float(num1) / num2)
  • That's almost correct, but you should get rid of that middle section from `a = int(num1 + num2)` to `d = int(num1 / num2)`. You don't need it, and you don't do anything with the values it calculates. – PM 2Ring Oct 05 '17 at 13:09
  • BTW, in Python 3 you don't need to do `float(num1) / num2`, you can just do `num1 / num2` and it will give the correct result. – PM 2Ring Oct 05 '17 at 13:10
  • @wilnnyadreu print a,b,c and d respectively not the operations they represent. – brddawg Oct 05 '17 at 13:21