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

The Challenge - my solution (including extra credit!)

Hey guys, my first post in here :). I have decided to share my solution to this challenge. Feel free to post negative or positive comments.

The Challenge

#creating a lists with number of guesses
number_guesses = list()

#importing random number library and generating the random number
import random
random_number = random.randint(1, 10)

#asking user to guess the number
print("Guess the number between 1 and 10")
guess_number = int(input("> "))

#while loop is running until the number is guessed correctly
#number of guesses is stored in a list, we are not interested in numeric values just how many numbers there are in the list
#printing a statement with number of guesses already made (length of the list)

while True:
  if guess_number == random_number:
    number_guesses.append(guess_number)
    print("Well done! You have guessed the number! Your have tried to guess {} time(s). You can try to guess only 3 times ".format(len(number_guesses)))
    break

#running the loop if number user typed in is lower than the random number    
#number of guesses is stored in a list, we are not interested in numeric values just how many numbers there are in the list
#printing a statement with number of guesses already made (length of the list)
#breaking the loop after 3 guesses

  elif guess_number < random_number:
    number_guesses.append(guess_number)
    print("Your guess is too low, try again! Your have tried to guess {} time(s). You can try to guess only 3 times".format(len(number_guesses)))
    guess_number = int(input("> "))
    if len(number_guesses) == 3:
      print("You have failed to guess the number")
      break

#running the loop if number user typed in is higher than the random number    
#number of guesses is stored in a list, we are not interested in numeric values just how many numbers there are in the list
#printing a statement with number of guesses already made (length of the list)
#breaking the loop after 3 guesses

  elif guess_number > random_number:
    number_guesses.append(guess_number)
    print("Your guess is too high, try again! Your have tried to guess {} time(s). You can try to guess only 3 times".format(len(number_guesses)))
    guess_number = int(input("> "))
    if len(number_guesses) == 3:
      print("You have failed to guess the number")
      break

[MOD: added ```python formatting -cf]

2 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,468 Points

Nice job Francisek! Welcome to posting on the forum.

I have 3 feedback points. The first is readability of the code. Please look at the Python style guide PEP-0008. Specifically, using 4-space indent, avoiding long line length, and indenting comments within the while loop to align with the statements.

The second is to increase the DRY-ness of your code by not repeating yourself.. Notice how the input() is used in three places, and the check for guesses is in two places. With a simple rearranging of your current code, these can both be eliminated by moving the prompt to the top of the while loop and moving the check for guess count to the bottom of the while loop.

The third is to create a MAX_GUESSES variable to replace the "3" and a MAX_RANGE variable to replace the "10". These will allow a easier customization of your code for other guessing scenarios. Maybe you could prompt the user for the max range and use a formula to set the max guesses based on the range.

I've reformatted most of the code, and thinned the redundant comments. Does this seem more readable?

# creating a lists with number of guesses. we are not interested in numeric
# values just how many numbers there are in the list
number_guesses = list()

# importing random number library and generating the random number
import random
random_number = random.randint(1, 10)

# asking user to guess the number
print("Guess the number between 1 and 10")

# while loop is running until the number is guessed correctly
while True:
    guess_number = int(input("> "))
    if guess_number == random_number:
        number_guesses.append(guess_number)
        print("Well done! You have guessed the number! " +
              "You have tried to guess {} time(s). " +
              "You can try to guess only 3 times ".format(len(number_guesses)))
        break
    # running the loop if number user typed in is lower than the random number
    # printing a statement with number of guesses already made (length of the list)
    elif guess_number < random_number:
        number_guesses.append(guess_number)
        print("Your guess is too low, try again! " +
              "You have tried to guess {} time(s). " +
              "You can try to guess only 3 times".format(len(number_guesses)))
    # running the loop if number user typed in is higher than the random number
    # printing a statement with number of guesses already made (length of the list)
    elif guess_number > random_number:
        number_guesses.append(guess_number)
        print("Your guess is too high, try again! " +
              "You have tried to guess {} time(s). " +
              "You can try to guess only 3 times".format(len(number_guesses)))
    # breaking the loop after 3 guesses
    if len(number_guesses) == 3:
        print("You have failed to guess the number")
        break

Keep up the good work! And keep posting to the Forum!

Chris, thanks for the comments and suggestions - much appreciated. I am sort of new to coding (have started the whole thing couple of weeks ago, so there is lots of stuff to learn :) ). The link you have mentioned in your post (Python style guide PEP-0008. ) does not seem to work. Can you check it once again ? Thanks, Frank