Click here to Skip to main content
15,611,036 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello, I am working on a Guess the Number Python Project and I want to know if the provided sample code is correct or not. I have taken this source code on techgeekbuzz( Cool, Fun & Easy Python Projects with Source Code in 2022[Updated][^] and in this python project, I want to generate a random number and the user will have only 3 chances to guess. and after the user each attempt, the user will get a Popup message about how the user is close to the answer, if the user completed all the chances, the system will display the correct answer. Can anyone tell me, if Is it right or provide some other source code for the number-guessing project?

What I have tried:

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<number:
                        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:
        print('Nope. The number I was thinking of was ' + str(number))

while playagain=="yes" or playagain=="y" or playagain=="Yes":
    print("Do you want to play again?")
Updated 16-Nov-22 2:51am
Richard MacCutchan 16-Nov-22 9:15am    
If it works it is correct, if it does not work then it is not correct. :simples:

If you have an actual problem with it, then please use the Improve question link above, and add complete details of what is not working.

1 solution

While we are more than willing to help those that are stuck, that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

And we certainly aren't here to check if code you are planning as passing off as your own work fits the assignment, and if not tell you why not. Just finding code on the internet isn't development, and it isn't learning: which is what homework is set for.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900