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

Fable Turas
Fable Turas
9,405 Points

My solution. Python basics number challenge

The one thing I am having an issue with is: if I try to stop the program with ctrl+c, the try/except test to see if the number is and integer (in check_input()) causes an exception that triggers the loop back to asking for correct input. My code:

import random

def start_game():
  print("Try to guess our random number")
  print("The number will be between 1 and 10")
  ask = "How many guesses would you like? > "
  return check_input(ask)


def check_input(question):  
  wrong_input = True

  while wrong_input:
    try: 
      test = int(input(question))
      wrong_input = False
    except:
      print("You need to enter a whole number. Try again")      

  return test



def make_a_guess(num_guesses):
  if num_guesses == 1:
    print("This is your last guess")
  else:
    print("You have {} attempts.".format(num_guesses))

  guess = "What is your guess? > "
  not_in_range = True

  while not_in_range:
    this_guess = check_input(guess)
    if 1 <= this_guess <= 10:
      not_in_range = False
    else:
      print("You need to enter a number between 1 and 10. Try again") 

  return this_guess



def play_again():
  yes_no = input("Would you like to play again? y/n > ")
  if yes_no == 'y':
    new_game()
    return 0
  else:
    print("Goodbye until next time then.")
    return -1



def play_the_game(num_guesses, our_number):
  i = 0
  i = num_guesses

  while i >= 0:
    if i == 0:
      print("You are out of guesses")
      print("The number was {}".format(our_number))
      i = play_again()

    else:
      this_guess = make_a_guess(i)

      if this_guess == our_number:
        print("Congratulations. You guessed it. The correct number was {}.".format(our_number))
        print("It took you {} tries to guess correctly".format(num_guesses - (i - 1)))
        i = play_again()

      elif i == 1:
        print("Sorry. That still wasn't the right number")

      else:  

        if this_guess < our_number:
          print("Our number is higher than {}.".format(this_guess))
        elif this_guess > our_number:
          print("Our number is less than {}.".format(this_guess))

    i = i - 1  



def new_game():
  our_number = random.randint(1,10)          
  num_guesses = start_game()
  play_the_game(num_guesses, our_number)



new_game()

Edited for syntax highlighting. :) Just for future reference, here's a post on how to post code to the forum.

1 Answer