Control Flow Code Challenges

Hi All,

Quick one - for the code challenges in flow control number 5 https://www.codecademy.com/courses/learn-python-3/articles/python-code-challenges-control-flow

Can someone please tell me why my code (the bold version) does not work. Instead of using this solution if (num1 + num2 != 10): I tried to define a variable called sum+of+both and then said num1 + num2 and returned sum_of_both. Why is this not possible? I get the answer 8, 10, 10

Thank you

def not_sum_to_ten(num1, num2):
if (num1 + num2 != 10):
return True
else:
return False

print(not_sum_to_ten(9, -1))
print(not_sum_to_ten(9, 1))
print(not_sum_to_ten(5,5))

My version

def not_sum_to_ten(num1, num2):
sum_of_both = num1 + num2
return sum_of_both

if varsum_of_both != 10:
return True
else:
False
print(not_sum_to_ten(9, -1))
print(not_sum_to_ten(9, 1))
print(not_sum_to_ten(5,5))

python does as you say so i guess you better say what you meant.

if you’re not sure what the things in your code do then go find that out before using them
if you’re not sure what things you’ve put in your code then you’re not really in control of it either and shouldn’t expect it to “work” until you know exactly what’s there

you’re the one who’s making it right so you’ll need to pay a bit of attention and demand to be in control

there’s no grand mystery in there, it just says the wrong thing, and since you wrote it, you’re the one who’s fixing it too!

Wow what a helpful and considered comment. Thank you. By the way before asking here I did try find the answer elsewhere. This is a forum for people to learn, if we were all advanced coders we wouldn’t be here. I asked why is it not possible, not is it incorrect.

No, read the code, that’s where the answer is.

That’s the thing. You don’t need help with this. Imagine you’re the computer. Execute the code, observe where you go wrong as you follow what your code says. And, while you’re doing this also check your confidence level on everything, if there’s anything you’re not entirely sure about then you need to solve that first.

If something doesn’t make sense in the first place then it’s not a matter of why it is or isn’t possible.

If you describe some action in english then you’d probably have an easier time checking whether it makes sense. Indeed, you could write down what you mean for this to do in english, and then match up your code to that. There needs to be code for each thing you wrote in english, and there must be no extra code. No more or less.

Similarly, you’d have an easier time answering whether something’s possible if you describe it in english. And because you’re in the same physical world as your computer, if you determine that it is possible then yeah it’s possible. Same laws of physics, same things are possible.

So you likely meant to do one thing, then wrote a bunch of code without paying too much attention, so that the code doesn’t match what you said and therefore does something else. So, a bit of attention to detail and a bit of demand of control over your code, and you’ll have it doing what you meant.

There’s also nothing stopping you from writing out messages to find out what order things are being done in or write out partial results. Your program isn’t some box that makes whirring noises and then spits out a wrong answer. It carries out a bunch of operations that you can observe if you choose to look, and then you’ll see where it goes wrong and probably be able to adjust it without much difficulty.

1 Like

If that’s your whole description then you’re completely omitting the comparison to 10. You don’t need me to tell you that though. Maybe you forgot what your function was supposed to do?

You did a similar thing in another thread: Using Modulo with variables

I don’t think you quite understand the purpose of the return keyword…

Part of learning any new thing is finding out if there is more than one solution to something. Im sure you would agree in programming there is often more than one way to do something. That is what I was asking at the beginning of this thread. Maybe I don’t understand the purpose of return 100%, but are you the programming police or are you here to assist people in a positive and constructive way? The tone of both of your answers would suggest its not the latter and seems to make new learners hesitant to ask a question. Quite a negative experience right now, and not a very positive reflection on CA. Thank you.

1 Like

If you look at your question, it’s not too clear what you’re asking. The code is malformatted, the thing you’re asking about is described as “this” and the question is “why”.

Maybe, then, you’re expecting some service-oriented happy response. Meanwhile, other random people on the internet are trying to have a discussion with you, and the question you made is reflected in that, and you’re not finding that to be sufficiently pleasurable.

So, hey. You could make it abundantly clear what you’re asking and you’ll get responses that are more straight to the point. And it helps a whole lot if it’s also clear why you’re not able to figure it out, because if something seems like you can work it out yourself then people will start thinking something else is wrong if you’re not seeing it and trying to address that instead.

How do you address someone off the street about a coding problem? It’s like that.

I was simply asking - is it possible in Python to add two parameters (num1 + num2) within a variable (sum_of_both) as I tried to do. Is this something that is do able? Or is the only one correct way - if (num1 + num2 != 10)?

I tried to google this before asking here and could not find the answer, so thought Id ask in a forum. Do you realise that I have literally been learning Python for 3 days and tried to ask this the best way I could. Yet after two days of discussion I am still trying to get an answer.

Same thing as me coaching tennis and you asking me why you cant hit a serve. I can see the problem, and you ask me why but instead of helping you (someone who has lifted the racquet for the first time 3 days ago) I tell you to work it out yourself, or telling you that you should ask me in a better way. Not sure what help that would be.

You can add two things and assign a variable to the result.
But that’s testable, isn’t it?

a = 5
b = 3
c = a + b
print(a)  # does this show 8?
c_not_ten = c != 10
print(c_not_ten)  # is c different from 10?

To use a binary (two arguments) operator such as +, you’d write + and then on each side you’d put an expression. As a whole that is an expression.

For assignment, you’d write a name, =, and an expression.

If you’re going to test something you’ll probably want to remove all other code that isn’t related to the thing you’re testing, at least if you’re not entirely sure it won’t interfere.

1 Like

Python, same as other languages, provide a number of patterns with special meaning to the language. That’s what syntax is.

If you learn each such pattern individually, then you’ll also see how they fit together.

Things involved here are assignment statement, and add operator. You would look these up indivudually.

Similarly you might look up what a return statement does and how that relates to a def statement.

Using several things in the same place doesn’t make the individual parts behave any differently, there’s no “greater than the sum of the parts” kind of things going on. It’s like lego bricks, and you fit them together. Make sure you understand each brick that you’re using. Refuse to use anything you don’t know what it is until you’ve looked it up.

Some people will start guessing a whole lot, writing things and praying it’ll mean something. That’s like shaking a box of legos for a while and hoping that the thing painted on the box will now have been assembled inside. You get there with deliberation and control, nothing else.

1 Like

Yes, there are frequently cases where a problem can be solved in a variety of ways. Nobody is faulting you for attempting a solution which differs from the “stock” answer provided by Codecademy. If anything, looking for alternatives is a good thing - you might find a more efficient method for solving the problem.

No, not the programming police, but we are trying to help you.

The reason your proposed solution doesn’t work is because, like I said, you are incorrectly using the return keyword.

The purpose of return is to end your function, and pass a value back to whatever called it. You simply can’t do this:

def not_sum_to_ten(num1, num2):
  sum_of_both = num1 + num2
  return sum_of_both

and then carry on with more code in the same indented block. Python will get to your first return, and will do as you’ve told it - return the value, and end the function. The rest of what you’ve written will never run.

def not_sum_to_ten(num1, num2):
  sum_of_both = num1 + num2
  return sum_of_both            # this is as far as python gets
  
  if varsum_of_both != 10:      # this line,
    return True                 # this line,
  else:                         # this line,
    False                       # and this line will never be reached

We are here because we want to try and help you understand coding, and to help you along through the course material if you get stuck.

Quite a lot of the time, rather than solve the problem for you or give you the answer to an easily fixable problem, we’ll instead give you a hint and nudge you in the right direction - like I tried to by mentioning the return keyword.

We’re not trying to confuse you, or to keep you from the answer, and we’re definitely not trying to put learners off asking questions. It’s just we think that you’ll learn more if we give you hints and you arrive at the solution on your own than if we just hand over the answer.

Hope that helps and answers your question, and I hope you won’t be put off coming back and asking more questions. :slight_smile:

1 Like

Hi,

Thank you for informative response. It is appreciated.

This helped me understand the return function better and where I was not understanding it 100%.

I understand what you were trying to do, in my case I had tried to find the answer and watched videos on other sites too. After spending some time I thought instead of wasting more time, I would ask here. Remember, when you know something well - it is not always obvious to others who are just starting out.

Thanks for the explanation.