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 Python Basics (Retired) Pick a Number! Any Number! The Solution

OH! My GOD! Kenneths code is far more superior then mine. please compare them both and give me feedback.

mine:

import random

random = random.randint(1, 10)  


while True:
  print("Guess the right number between 1 and 10")
  ask = input("> ")
  user_num = int(ask)
  List = []
  ask.append(List)
  if user_num == random:
    print("Congrats! you have gussed the right number")
    break
  elif user_num > random:
    print("Too high")
    continue
  elif user_num < random:
    print("Too low")
    continue

print("It took you {} tryes to guess the number".format(len(List)))

Kenneth:

import random

rand_num = random.randint(1, 10)
guessed_nums = []
allowed_guesses = 5

while len(guessed_nums) < allowed_guesses:
  guess = input("Guess a number between 1 and 10: ")

  try:
    player_num = int(guess)
  except:
    print("That's not a whole number!")
    break

  if not player_num > 0 or not player_num < 11:
    print("That number isn't between 1 and 10!")
    break

  guessed_nums.append(player_num)

  if player_num == rand_num:
    print("You win! My number was {}.".format(rand_num))
    print("It took you {} tries.".format(len(guessed_nums)))
    break
  else:
    if rand_num > player_num:
      print("Nope! My number is higher than {}. Guess #{}".format(
        player_num, len(guessed_nums)))
    else:
      print("Nope! My number is lower than {}. Guess #{}".format(
          player_num, len(guessed_nums)))
    continue

if not rand_num in guessed_nums:
  print("Sorry! My number was {}.".format(rand_num))

3 Answers

Keith Whatling
Keith Whatling
17,759 Points

Hi Ibs,

Don't worry! you will pick it up.

Kenneth's code looks a lot more but he is checking a few things to stop the program crashing and also giving the user a max number of tries.

Try is saying 'try this and if it goes wrong look at what 'except' is doing.

  try:
    player_num = int(guess)
  except:
    print("That's not a whole number!")
    break

then he checks that the user has entered a number in the range. It just looks more.

Reading over your code there is only one thing I would change. the variable you have called 'List'. Get out of the habit now of calling anything that is a list List or lst. The word list is reserved in python, call it something like guesses or tries and always lower case.

Have a think about how you could expand the program, it is within your reach!

Here is a link to a guess the number game on codeskulpter part of the Coursrea python course at Rice. It has lots of bells and whistles like keeping score etc.

I took this online course and I learnt a lot about coding for games and maths and yawn.... Kennith will show you how to build real world things that can really get you a job. Stick with it! You can do it, and the feeling you are going to get when you crack it, I do this silly little dance but that's way to much info.

Some ideas for you code.

import random


"""add a input so you can change the range of numbers. you can then store the max number
in a variable and test to see if any of the entries are with in range."""

random = random.randint(1, 10)  
guesses = []

while True:
    print("Guess the right number between 1 and 10")
    ask = input("> ")
    user_num = int(ask)

    # Expand code here.

    guesses.append(user_num)
    if user_num == random:
        print("Congrats! you have gussed the right number")
        break
    elif user_num > random:
        print("Too high")
        continue
    elif user_num < random:
        print("Too low")
        continue

"""make a test to see if its the minimum amount of tries needed. so if the random number is 8, 
math's wise you would look for that by deviding by 2 all the time. So, 5,7, and then either 8 or 9 
and you would could say, 4 tries is good, 3 tries is great"""

print("It took you {} tryes to guess the number".format(len(guesses)))

Thanks man. but what is Coursera? and can you learn programming in python there?.

Keith Whatling
Keith Whatling
17,759 Points

You can learn python at coursera and other things too, however, I find the tuition here better and there is a lot of focus on maths and programming at coursrea, something that is not really needed in most function programming, its only really games or statistical/fiscal software and its really the games where the maths is hard, the stats stuff is done for you.

so what I am saying is you spend a lot of time messing around getting the maths right, when you are meant to be learning how to code.

I have doe Lynda.com too and this is by far the best. Stick with Kenneth he is really good and he is there to help too.

Thanks for the advice, i think i will stick with kenneth.