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
Michal Janek
Front End Web Development Techdegree Graduate 30,654 PointsMy Python Ran.Number acts weird
Once you execute this code it should ask the user for his guess for random number already generated. It is pretty standard random quiz generator which I made up some time later after taking Python Basic Course making sure I understood the concept. What is super weird and the reason I am asking here is this: Once you get the number right and you say No for question whether you want to play again it will quit as intended. However if you ask first time yes and second time no, the program asks twice for user input no/yes and if you enter no second time, it will quit. NO idea why. Help?
P.S. I know this can be made shorter and also there are some parts that can be made easier - normally I would clean it up right away - however this mystery just keeps bugging me and I cannot focus on this code anymore without thinking about it.
import random
def game(x,y):
con = True
count = 0
ranNum = random.randint(x,y)
while con:
userInput = int(input("Take a guess between these numbers {} and {}: ".format(x,y)))
count +=1
if userInput == ranNum:
print("You got it!")
print("Number of tries: {}.".format(count))
print("Play again?")
while True:
again = input("> ")
if again.lower() == "yes":
game(x,y)
elif again.lower() == "no":
con = False
break
elif userInput < ranNum:
print("Wrong! Try higher!")
elif userInput > ranNum:
print("Wrong! Try lower!")
game(5,10)
john larson
16,594 Pointsouch
2 Answers
Umesh Ravji
42,386 PointsHi Michal, the problem here is that inside your loop you are calling the game method again, creating another game loop. If you say yes 5 times, then you will have to say no 5 (or maybe 6) times to actually get out of the game.
Play again?
> no
> no
> no
> no
> no
Updated
import random
def game(x,y):
con = True
count = 0
ranNum = random.randint(x,y)
while con:
userInput = int(input("Take a guess between these numbers {} and {}: ".format(x,y)))
count +=1
if userInput == ranNum:
print("You got it!")
print("Number of tries: {}.".format(count))
print("Play again?")
while True:
again = input("> ")
if again.lower() == "yes":
ranNum = random.randint(x, y) # regenerate random num
count = 0 # reset count variable
break # go back to the outer loop
# game(x,y)
elif again.lower() == "no":
con = False
break
elif userInput < ranNum:
print("Wrong! Try higher!")
elif userInput > ranNum:
print("Wrong! Try lower!")
game(5,10)
Michal Janek
Front End Web Development Techdegree Graduate 30,654 PointsThank you very much for clearing the issue :)
Kenneth Love
Treehouse Guest TeacherUmesh Ravji has the right idea: you're constantly running the game function. I'd recommend trying return game(x, y) instead of the while True variant that Umesh suggested, but their suggestion is 100% appropriate.
(p.s. Alexander Davison, please stop tagging me on every post. We have a large and active community and I like to give other people a chance to answer questions. Often they'll skip answering a question once they see that a teacher or other staff member has been alerted to it.)
Alexander Davison
65,469 PointsOh ok. :)
Michal Janek
Front End Web Development Techdegree Graduate 30,654 PointsI will try the recommendation as well will see which will feel more natural to me. Thank you I appreciate it and sorry for being 'dragged' in :D
Alexander Davison
65,469 PointsAlexander Davison
65,469 PointsTagging Kenneth Love, I spent 15-20 minutes staring at this program and I couldn't find the issue.