Bug in Control Flow practice concepts?

I AM trying to complete a “Practice Concepts” session from the Python 3 “Control Flow” lesson. But I am unable to pass a question. Please help? The link to the problem is below …
https://www.codecademy.com/practice/tracks/learn-python-3/modules/learn-python3-control-flow

THE question is below …
Create a function called divisible_by_ten() that has one parameter named num.

The function should return True if num is divisible by 10, and False otherwise. Consider using modulo operator % to check for divisibility.

MY CODE solution seems to be fine because no Errors come up in the terminal output when I run it. See below …
def divisible_by_ten(num):
if num % 10 == 0:
print(“True”)
else:
print(“False”)

WHEN I run my code, the output in the terminal is as seen below …
True
None
False
None

I DO find the “None” outputs a bit peculiar. Where did those come from? I was expecting only “True” or “False” outputs.

BUT I think there is something wrong with the additional instructions in the terminal. See below …

Uncomment these print() function calls to test your divisible_by_ten() function:

#print(divisible_by_ten(20))

should print True

#print(divisible_by_ten(25))

should print False

IS there a bug? When I uncomment the functions as instructed and click on the “Check Answer” button, I am told that divisible_by_ten(50) should have returned True , and it returned None … BUT where did divisible_by_ten(50) come from? I don’t see it anywhere to be uncommented! I only see two functions that I was asked to uncomment.

WHAT’S going on? Please help? Thank you.

ALSO one more thing, I’ve gone back into this post to unsuccessfully try to edit the enormously large and bolded “Uncomment these print() function calls to test …” and “should print True” and “should print False.” I have no idea why these three statements that I copied and pasted directly from the practice concept link look like this on this community forum. Sorry.

Hi,
It’s expecting you to return true or false, not print them in your function.
I.e.

def divisible_by_ten(num):
  if num % 10 == 0:
    return “True”
  else:
    return “False”

Without a return statement your function is returning None by default - which is where that comes from.
The ‘divisible_by_ten(50)’ is probably a hidden test they’ve added to make sure we’re not just ‘hard-coding’ an answer.
Something like,

if (num == 20)
  return True
else:
  return False

just so it passes the test we can see rather than trying to solve the problem.

As For The text, that’s markdown
You can use special characters to help empahsise what you say.

Hope that helps.

HI pluginmaybe. Thank you for your response. I restarted the Practice Concepts again for the Python 3 Control Flows lesson and was provided with yet another question similar to the one that you helped me to answer. My code for this new question followed your example (thank you!) … but the same problem as before arises. Please see below for the link …
https://www.codecademy.com/practice/tracks/learn-python-3/modules/learn-python3-control-flow

THE QUESTION I am given is below …
Create a function named twice_as_large() that has two parameters named num1 and num2.
Return True if num1 is more than double num2. Return False otherwise.
twice_as_large(20, 10) should have returned False , and it returned False

MY CODE following your example (which I think makes sense, thank you!) is below …
def twice_as_large(num1, num2):
if num1 > num2 * 2:
return “True”
else:
return “False”

I AM ASKED to uncomment the following to test that my code works …
#Uncomment these function calls to test your twice_as_large function:
#print(twice_as_large(10, 5))
#should print False
#print(twice_as_large(11, 5))
#should print True

WHEN I run my code, hooray the False and True answers match.

BUT when I click on the Check Answer button, the same problem as in my original post arises and the following message is shown as seen below …
twice_as_large(20, 10) should have returned False , and it returned False

I DO NOT pass the question. Please help?

Answering my own question! Maybe someone can double check what I think was the problem?

MY PROBLEMATIC CODE was …
def twice_as_large(num1, num2):
if num1 > num2 * 2:
return “True”
else:
return “False”

I THINK the reason that the Check Answer button prompted the message: twice_as_large(20, 10) should have returned False , and it returned False … is because I had “True” and “False” as strings, not, erm, boolean values. Am I using the correct term to describe this True and False without quotation marks?

MY REVISED code which allowed me to pass the question was without the quotation marks as seen below …
def twice_as_large(num1, num2):
if num1 > num2 * 2:
return True
else:
return False

HOORAY?

1 Like
>>> type("True")
<class 'str'>
>>> type(True)
<class 'bool'>

In Python, returning a bool is synonymous with returning 0 (False) or 1 (True).

>>> True == 1
True
>>> False == 0
True

But those are integers, how can that be? Python has defined it as such, so that,

>>> isinstance(True, int)
True
>>> isinstance(False, int)
True

That means that the bools have int methods, so we can do arithmetic with them.

>>> True + True
2
>>> True - True
0
>>> True + False
1
>>> False + False
0
>>> True * True
1
>>> True * False
0
>>> 1 / (True + True)
0.5
>>> 

A good lot of concept demonstration code is very trivial and almost pointless, except for what it reveals:

>>> def rib():
        return bool(choice(range(2)))

>>> for _ in range(10):
        a, b = rib(), rib()
        try:
            print (1 / (a + b))
        except ZeroDivisionError:
            print (0.0)

        
1.0
0.5
0.5
0.5
0.0
1.0
1.0
1.0
0.5
0.0
>>> 

Fun stuff, eh?

1 Like

Above, we’re meant to have included the opening line,

from random import choice

Thanks mtf! Your first four points in your reply were very clear and helpful! But you lost me on your last point about concept demonstration code being trivial except for what it reveals…mainly because I have not yet learned what the following functions are … choice … _ … try … except … !

1 Like

All in good time. Eventually these things will come up in the lessons. For now, see if you can reason out what each component is doing. Search is allowed.

1 Like

To be fair, choice is a module of the random module`. It is a randomly chosen value from a group of values, such as a range or a list, or any iterable. They don’t have to be in sequence since it is just picking one at random like drawing a card, any card from a deck of cards.

We can also write it like,

import random
c = random.choice(range(1, 11))

In our earlier example above we used, range(2) as our iterable, which contains only two values, 0 and 1.

The three possibilities with a and b are,

  • a AND b gives 1 / 2
  • a OR b gives 1
  • NOT a AND NOT b gives zero

There will be a unit or some lessons at least that cover exception handling, that is, trapping cases where we suspect an error may be thrown. We wrap only the code that might raise the exception in a try block, and trap the exception in an except block.

try:
    # when a and b as both zero it will raise a division by zero error
    print (1 / (a + b))
except ZeroDivisionError:
    # code to execute in case of exception
    print (0.0)

This is how we can handle known possible exceptions so the interpreter doesn’t raise a fatal exception and kill the program.

2 Likes

I am bookmarking this mini-lesson from you to reference in the future when I encounter … choice … random … etc … in the Python 3 lesson modules!

1 Like

We might have a circuit for LEDs based on the HSL color scheme. 0 is red, 120 is green, and 240 is blue. So we connect the leads to three LEDs, and depending upon the value computed by our process above, we light up one of the bulbs (or set the color of a circle on the screen using HSL syntax (a function)).

hue = 240 * (1 / (a + b)) # pretend one outcome can be zero
color = hsl(hue, 100, 50)

Well, this looks like a tool looking for a job, or an idea looking for a purpose. Funny how that works.

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.