# 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.

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.

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.