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

Python

Can I get some feedback on my attempt before the video?

I believe the only thing I did NOT consider when making this game is if the player enters a float or a number higher than 10.

import random
print("Let's play a game!")
print("Try to guess the right number, pick 1 through 10!")

user_attempt = []
correct_number = random.randint(1,10)

def attempts():
  user_attempt.append(user_number)
  print("You have attempted {} times.".format(len(user_attempt)))
  if len(user_attempt) == 5:
    print("I'm sorry, the correct number was {}.".format(correct_number))
    exit()

while True:
  user_number = int(input("< "))
  if user_number == correct_number:
    break
  elif user_number < correct_number:
    print("Too low, try again!")
    attempts()
    continue
  elif user_number > correct_number:
    print("Too High, try again!")
    attempts()
    continue

print("Congratulions! The correct number is {}.".format(correct_number))

1 Answer

You may get an Exception on this statement -

user_number = int(input("< "))

  • utilize WorkSpace to test it out. If so, Try/Except can handle in this scenario -

try: user_number = int(input("< ")) except: print("Invalid entry, try again!") attempts() continue