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 trialGilang Ilhami
12,045 PointsOther framework Does this code only work in Workspace? I tried on another framework but it doesn't work
import random
# safely make an interger
# limit the number of guesses
# too high
# too low
# play again
def game():
# generate a random number between 1 and 10
secret_num = random.randint(1, 10)
guesses = []
while len(guesses) < 5:
try:
# get a number gueast for the player
guess = int(input("Guest a number between 1 and 10: "))
except ValueError:
print("That is not a number")
else:
# corporate guest to secret number
if guess == secret_num:
print("That's right! {}!".format(secret_num))
break
elif guess < secret_num:
print("Higher than {}".format(guess))
elif guess > secret_num:
print("Lower than {}".format(guess))
else:
print("That's not it")
guesses.append(guess)
else:
print("You know, it's actually {}.".format(secret_num))
play_again = input("Play again? Y/n ")
if play_again.lower() != "n":
game()
else:
print("Game Over")
# print a hit or a miss
game()
1 Answer
Nate Sturgeon
2,255 PointsIt should work... unless the other framework you're trying it in is Python 2, in which case you'll have to change some syntax for it to work (eg all print statements must not be in parenthesis in Python 2).