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
Mateusz Bijakowski
1,335 PointsNumber game reversed - own version, stupid mistake.
Hi, I made my own version of game and it was working with small error at the end. Then I changed something and whole thing is crashing, I cant find it and I'm honestly giving up.
import random
#game
def game():
# variables
guesses = []
up_to = []
from_to = 0
helper = []
secret_num = []
# instructions and GET number
print("""In this game computer will try to guess your number, you will be helping with:
H for "too high"
L for "too low"
""")
try:
up_to = int(input("""
Computer will have to guess a secret number from 1 to "X"
How big should "X" be?
> """))
except ValueError:
print("This isn't a number!")
else:
secret_num = int(input("""
Choose secret number for computer.
> """))
# NUMBER of attempts
attempts = []
try:
attempts = int(input("""
How many attempts should computer have?
> """))
except ValueError:
print("This isn't a number!")
else:
print("""Computer will try to guess a number from 1 to {}, in {} attempts.
Your secret number is {}, remember it so you can help him.""".format(up_to,attempts,secret_num))
# "Try" part of the game
while attempts > len(guesses):
guess = random.randint(from_to, up_to)
print("'I guess {}!'".format(guess)
if guess == secret_num:
guesses.append(guess)
print("""
Congratulations, computer guessed you number {} on {} attempt""".format(secret_num,len(guesses)))
break
# ask for "to low" or "to high"
elif guess != secret_num:
helper = input("""Was the number H or L
> """)
guesses.append(guess)
if helper.lower() == 'h':
from_to = guess
elif helper.lower() == 'l':
up_to = guess
continue
# YOU WIN message
else:
print("Computer didn't guessed your number")
play_again = input("Do you want to play again? Y / n ")
if play_again.lower() != 'n':
game()
else:
print("""
Bye!""")
game()
3 Answers
Name:GoogleSearch orJonathan Sum
5,039 Pointsit looks like you have one error in the ""Try" part of the game" block. You didn't close the print function with )sign. This one-> print("'I guess {}!'".format(guess) after the fixed,print("'I guess {}!'".format(guess)) If you have more problems ,ask!
You made a nice number game. I played it after the fixed! (unlike me, i am just a noob.
#before the fixed
# "Try" part of the game
while attempts > len(guesses):
guess = random.randint(from_to, up_to)
print("'I guess {}!'".format(guess) #here missing a ) siging for closing the print function.
if guess == secret_num:
guesses.append(guess)
print("""
Congratulations, computer guessed you number {} on {} attempt""".format(secret_num,len(guesses)))
#after the fixed by adding ")" sigin after the .format(guess) ,it becomes this:
# "Try" part of the game
while attempts > len(guesses):
guess = random.randint(from_to, up_to)
print("'I guess {}!'".format(guess)) # i added the ")" sign for you
if guess == secret_num:
guesses.append(guess)
print("""
Congratulations, computer guessed you number {} on {} attempt""".format(secret_num,len(guesses)))
Name:GoogleSearch orJonathan Sum
5,039 PointsI have played your game two times. It is a pretty interesting game. In the first time i played, i told the computer too low, the computer kept guessing a lower number, instead guessing a higher number. the sceond time i played ,the comeputer just kept guessing the number 9 ,no matter i told the computer his number is too low or too high. However, it is pretty nice game.
#First round
In this game computer will try to guess your number, you will be helping with:
H for "too high"
L for "too low"
Computer will have to guess a secret number from 1 to "X"
How big should "X" be?
> 10
Choose secret number for computer.
> 5
How many attempts should computer have?
> 4
Computer will try to guess a number from 1 to 10, in 4 attempts.
Your secret number is 5, remember it so you can help him.
'I guess 3!'
Was the number H or L
> L
'I guess 3!'
Was the number H or L
> L
'I guess 1!'
Was the number H or L
> H
'I guess 2!'
Was the number H or L
> H
Computer didn't guessed your number
Do you want to play again? Y / n Y
#Second round
In this game computer will try to guess your number, you will be helping with:
H for "too high"
L for "too low"
Computer will have to guess a secret number from 1 to "X"
How big should "X" be?
> 10
Choose secret number for computer.
> 5
How many attempts should computer have?
> 6
Computer will try to guess a number from 1 to 10, in 6 attempts.
Your secret number is 5, remember it so you can help him.
'I guess 9!'
Was the number H or L
> H
'I guess 9!'
Was the number H or L
> L
'I guess 9!'
Was the number H or L
> L
'I guess 9!'
Was the number H or L
> H
'I guess 9!'
Was the number H or L
> L
'I guess 9!'
Was the number H or L
> H
Computer didn't guessed your number
Do you want to play again? Y / n
Mateusz Bijakowski
1,335 PointsThank you for feedback, I was in middle of tweaking it when I brooked it. I hope to finish with final version where computer isn't just random guessing but check numbers in the middle.
Mateusz Bijakowski
1,335 Pointsall fixed, I will be developing it later, for now I going back to course. If anyone is interested. https://github.com/Landsil/number_game_rev/blob/master/number_game_rev.py
Name:GoogleSearch orJonathan Sum
5,039 PointsNice! i will try it later.
Mateusz Bijakowski
1,335 PointsMateusz Bijakowski
1,335 PointsThank you, I must have removed by mistake.