Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Joshua Howard
Joshua Howard
13,617 Points

Wrong number of prints

Okay, so I have a few questions regarding this exercise. And i'm pretty sure it's just me having a brain-fart. But, why is the argument (num) in our function? I'm not sure how or if it's even supposed to be used.

Second, i'm getting an error telling me that the number of prints are wrong. So i'm assuming this has something to do with the counter not being implemented correctly. But i've spent about 30 minutes or so trying to figure it out. But to no avail.

Lastly, I don't know why the "return not num % 2" is actually doing. I mean, I understand what it's saying, but don't understand it's purpose. I hope you guys understand what i'm trying to say.

import random

start = 5
def even_odd(num):
    while start > 5:
        ran = random.randint(1, 99)
        if rand % 2 == 0:
            print("{} is even".format(ran))
        else:
            print("{} is odd".format(ran))
    start -= 1
    return not num % 2
Joshua Howard
Joshua Howard
13,617 Points

Okay, i've figured out that the function is actually figuring out if it's even or odd using modulo. So the if condition in my statement with the function. Also saw that I didn't actually have to use "while start > 5" and could just use "while start"... is that just a mechanic of python that allows me not to require that?

Okay, so I am pretty new to python and I am unsure what return not num % 2 means? if you can explain it that'll be great to know. To answer your questions:

  1. num is used as a parameter. it can be any variable. you can have def even_odd(anyNumber) and it ll work the same.
  2. There is a problem with your code. Right now, your code is never going to end when you call the function. You are never reaching start -= 1.

  3. i wouldn't say there is a problem, but you are hard coding. Lets say you don't know what the number start is going to be because you asked for an input using start = raw_input() [user sends a number in via console] you cannot say when start > 5 because the user can put in 10 instead.

import random
start = 5
def even_odd(num):
    while num > 0: #by using num >0  your code is more versatile. instead of 5 it can 10 and it will print the appropriate amount of time without having to change your code 
        rand = random.randint(1,99)
        if rand % 2 == 0:
            print("{} is even".format(rand))
        else:
            print("{} is odd".format(rand))
        num -=1 # inside the while loop so that num can decrease by one, otherwise your code never leaves while loop
    return not num%2

print even_odd(start)

When i first started coding with java a year back I used to run into problems like this all the time. The best way to catch these mistakes is to add print statements throughout your code so you know when your code leaves the while loop, or what was the last step before it crashed.

1 Answer

Steven Parker
Steven Parker
243,656 Points

:point_right: It looks like you may have mixed up the function with the program.

You didn't provide a link to the course or challenge, but if I remember correctly, this is from a challenge that provides you with the function, and asks you to write a program using it. You seem to have most of the right lines for the program, but mixed into the function, which is not the right place. All the new program code should be after the function.

My guess is that what you're really after would look more like this:

import random

def even_odd(num):
    return not num % 2

start = 5
while start:
    ran = random.randint(1, 99)
    if even_odd(ran):
        print("{} is even".format(ran))
    else:
        print("{} is odd".format(ran))
    start -= 1

So the even_odd function is left as it was provided, and only determines if a number is even. Then, the program tests 5 randomly selected numbers and prints out if they were found to be even or odd.

Joshua Howard
Joshua Howard
13,617 Points

Yes, I figured it out. What I was doing was stupid. Especially the line where I set the while loop to run as long as start was over 5. That is what was giving me the "wrong number of print" error. All fixed now. Thanks.