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 Python Basics (2015) Letter Game App Even or Odd Loop

Ashikur Rahman
seal-mask
.a{fill-rule:evenodd;}techdegree
Ashikur Rahman
Python Web Development Techdegree Student 932 Points

Can't solve this step :(

Hi! any one here to help me please. I can't build my login for this part. Your kind help will be appreciated.

Regards

even.py
import random
start = 5
def even_odd(num):
    # If % 2 is 0, the number is even.
    # Since 0 is falsey, we have to invert it with not.
    return not num % 2

2 Answers

You are really close! You created start = 5 and your while loop inside your even_odd function. You need to create that variable outside your function.

This should get you pass the task

import random

start = 5

def even_odd(num):
    # If % 2 is 0, the number is even.
    # Since 0 is falsey, we have to invert it with not.
    return not num % 2

while start > 0:
    num = random.randint(1, 99)
    if even_odd(num) == True:
        print("{} is even".format(num))
    else:
        print("{} is odd".format(num))
    start -= 1
Rich Zimmerman
Rich Zimmerman
24,063 Points

When it says "until start is falsey" means when it hits 0. So you want to create a while loop, that calls a random integer, prints whethers it even or odd, and then decrements "start" by 1. You don't want the function to return a value, you just want it to do something, in this case print a string.

import random
start = 5
def even_odd(num):
    # If % 2 is 0, the number is even.
    # Since 0 is falsey, we have to invert it with not.
    # Once start is <= 0, the while loop will be falsey and stop.
    while start > 0:
        random_int = random.randint(1, 99)
        if random_int % 2 == 0:
            print("{} is even".format(random_int))
        else:
            print("{} is odd".format(random_int))
        start -= 1
        # start is now 4, and the loop will keep running until start == 0
Rich Zimmerman
Rich Zimmerman
24,063 Points

My mistake. i forgot the format method

print("{} is even".format(random_int))

# and 

print("{} is odd".format(random_int))

updating my answer...

Ashikur Rahman
seal-mask
.a{fill-rule:evenodd;}techdegree
Ashikur Rahman
Python Web Development Techdegree Student 932 Points

Below is my code with that I'm trying to solve this step. But now it's throwing an indent error message. ridiculous :(

import random

def even_odd():
    start = 5
  # If % 2 is 0, the number is even.
  # Since 0 is falsey, we have to invert it with not.
  # Once start is <= 0, the while loop will be falsey and stop.
  while start > 0:
    random_int = random.randint(1, 99)
    if random_int % 2 == 0:
        print("{} is even".format(random_int))
    else:
        print("{} is odd".format(random_int))
  start -= 1
    # start is now 4, and the loop will keep running until start == 0

even_odd()