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 trialAdrian Brenton
8,110 PointsFinal Python basics challenge - some guidance would be much appreciated here! Thank you in advance
Hello all,
I've been trying to crack this challenge for a week, and have rewatched the course to see what I could glean from the material... it seems my logic is still flawed somewhere, and I'm confused as to why my code would make task 1 of this challenge fail, when previously it was passing fine.
I would very much appreciate some pointers as to where my logic and thought process could be going awry, so I can better understand how to solve this problem.
Many Thanks in advance
Best Regards
import random
start = 5
random_number = random.randint(1, 99)
def even_odd(random_number):
return random_number % 2
while start = True:
def even_odd(random_number):
return random_number % 2
even_odd(random_number)
if even_odd == 0:
print("{} is even".format(random_number))
else:
print("{} is odd".format(random_number))
start -= 1
else:
break
2 Answers
behar
10,799 PointsHey adrian! I can definetly see your idea with this code, but it seems that your not 100% clear on how functions work (which is totally understandable). You have defined a function twice, inside the loop and outside? Why, you only need to define a function once (good idea to do it outside the loop). Secondly, at the bottom of your code you are saying else: break? Why is this, you have already said when the loop should end, and that break statement isnt even inside a loop, this will cause an error when running the program. Now notice that your random number generator is outside of the loop, this means that your random number will always be the same, would you not like it to change it after each iteration of the loop? Lastly when defining your loop you have set the variable start to be the same as true? (((I have provided some updated code below to help you understand, but dont read it if you dont want the actual fix)))
-
-
-
-
import random
start = 5
def even_odd(random_number):
return random_number % 2
while start == True:
random_number = random.randint(1, 99)
if even_odd(random_number) == 0:
print("{} is even".format(random_number))
else:
print("{} is odd".format(random_number))
start -= 1
Please fill free to a ask more questions if this didnt suffice!
Adrian Brenton
8,110 PointsHey behar, thank you for your help! I can see some f the errors I made now, and I've tried your solution - unfortunately, it still tells me that 'task 1 is no longer passing'. Here is my updated code:
import random
start = 5
def even_odd(random_number): return random_number % 2 # If % 2 is 0, the number is even. # Since 0 is falsey, we have to invert it with not. while start != 0: random_number = random.randint(1, 99) if even_odd(random_number) == 0 print("{} is even".format(random_number)) else: print("{} is odd".format(random_number)) start -= 1
Adrian Brenton
8,110 Pointsoops - the code wasn't supposed to appear squashed!
Adrian Brenton
8,110 PointsUpdate - syntax error on my part - silly me!
Many thanks for you help behar!
behar
10,799 PointsGlad to help! You can mark the question as "solved" by sellecting a "best answer".