Grow your CSS skills. Land your dream job.

How to do try-except in python

  • # January 27, 2013 at 10:21 pm

    I have this code

    import random #bring in the random number
    import time
    number=random.randint(1, 200) #pick the number between 1 and 200

    def intro():
    print(“May I ask you for your name?”)
    name=input() #asks for the name
    print(name + “, we are going to play a game. I am thinking of a number between 1 and 200″)
    print(“Go ahead. Guess!”)

    def pick():
    guessesTaken = 0
    while guessesTaken < 6: #if the number of guesses is less than 6
    enter=input(“Guess: “) #inserts the place to enter guess
    try: #check if a number was entered
    guess = int(enter) #stores the guess as an integer instead of a string

    if guess< =200 and guess>=1: #if they are in range
    guessesTaken=guessesTaken+1 #adds one guess each time the player is wrong
    if guessesTaken<6:
    if guess
    print(“The guess of the number that you have entered is too low”)
    if guess>number:
    print(“The guess of the number that you have entered is too high”)
    if guess != number:
    print(“Try Again!”)
    if guess==number:
    break #if the guess is right, then we are going to jump out of the while block
    if guess>200 or guess<1: #if they aren't in the range
    print(“Silly Goose! That number isn’t in the range!”)
    print(“Please enter a number between 1 and 200″)

    except: #if a number wasn’t entered
    print(“I don’t think that “+enter+” is a number. Sorry”)

    if guess == number:
    guessesTaken = str(guessesTaken)
    print(‘Good job, ‘ + name + ‘! You guessed my number in ‘ + guessesTaken + ‘ guesses!’)

    if guess != number:
    number = str(number)
    print(‘Nope. The number I was thinking of was ‘ + number)

    while playagain==”yes” or playagain==”y” or playagain==”Yes”:
    print(“Do you want to play again?”)

    I don’t know why, but if i run this script, no matter what I enter for my guess, it goes with

    print(“I don’t think that “+enter+” is a number. Sorry”)

    Even if it is a number. I am not sure how to fix this

Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".