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
Haris Mikudim
1,642 PointsMy 'chances' does not stop at zero in the "PICK THE NUMBER ANY NUMBER GUESSING GAMES" can someone look at my code please
import random chances=5 print("Enter the MAGIC number from 1-10") print("You have 5 chances")
MAGIC=random.randint(1,10)
while True: guess=input("Enter Guess here: ") guess=int(guess) if guess==MAGIC: print("Winner") break elif guess<MAGIC: print("HIGHER") chances-=1 print("You have {} chances left".format(chances))
elif guess>MAGIC:
chances-=1
print("LOWER")
print("You have {} chances left".format(chances))
elif chances==0:
print("You lose")
break
1 Answer
Martin Cornejo Saavedra
18,132 PointsI found your error, you have to change "elif chances == 0" to "if chances == 0". There was no way the if logic would have entered on that case because the number is always equal, lower or higher that the magic number, so you have to check if it is zero separately.
import random
chances = 5
MAGIC = random.randint(1, 10)
while True:
guess = int(input("Enter guess here: "))
if guess == MAGIC:
print("win")
break
elif guess < MAGIC:
print("HIGHER")
chances -= 1
print("You have {} chances left".format(chances))
elif guess > MAGIC:
chances -= 1
print("LOWER")
print("You have {} chances left".format(chances))
if chances == 0 : #changed "elif" for "if"
print("You lose")
print("The Magic number was {}".format(MAGIC))
break
Haris Mikudim
1,642 PointsHaris Mikudim
1,642 PointsThanks for the help