Python Tutorial v3.2.5 Project 1 - Guess the Number

preview_player
Показать описание
5:16 Click to skip the optional program features.
8:34 Click to skip directly to the example program execution.

Project 1 outlines a simple game that you'll be able to program after mastering lessons 1-10 of the Python Tutorial Series of videos. It involves a simple game of "Guess the Number" using while statements and if statements to return if guesses are too high or too low. To be successful, concepts from the previous 10 tutorials must be implemented.

Keep in mind this tutorial is using an older version of Python, v3.2.5. You will need to click on the "View Older Releases" button to use this specific version. Using the newer versions will not be an issue at this point, but when the lesson proceeds to basic graphics, the Pygame Module we'll use does not support v3.3+ at this time.
Рекомендации по теме
Комментарии
Автор

@Julio Hernandez,

Actually, you did a pretty fantastic job on the program. I ran through it about a dozen times and didn't hit any bugs that I could see. All of the difficulty levels work working and had the gave me the correct number of guesses. In fact, your program was pretty well organized and it was simple to see the programming logic.

From a programming standpoint, I think you're on solid ground. The only habit I would get into that I didn't see in your program was the comments. As it stands right now, there were no comments that explained what different sections of the program did. On small programs like this, that may not be all too critical. On larger programs, it will make your life much easier to throw a comment or two in each block to explain what that block is doing. In addition, if you ever need help, comments help other programmers jump into your program and get a handle on the logic much quicker.

That's not a huge criticism, though. All in all, it was a solid program that worked really well. Keep it up!

xnavysteve
Автор

I'm ready. lets see if I've understood everything thus far.

Noldy__
Автор

# Guess your number
# Player chooses a number from 1 to 100
# Computer had 8 tries to guess the number
# Garry Beck 24/04/2015import random# Title and instructions
print("\t\t\t****GUESS YOUR NUMBER****")
print("\nPlayer enters a number from 1 to 100. "\
      + "Computer has 8 tries to guess the number.\n")
# Set initial variables
name = input("Please enter your name\n:")
number = int(input("%s, please enter your number: " % name))# checks player enters valid number between 1 and 100
while number <= 0 or number > 100: 
    number = int(input("%s, please enter a number from 1 to 100: " % name))tries = 1   # tries is number of computer guesses
high = 100
low = 1
guess_com = random.randint(low, high)         # guess_com = computer's guess
# Guessing loopprint("\nComputer's guess is:", guess_com)# 'while' loop modifies high and low variables based on user response
while guess_com != number and tries <= 7:
   
    # Player enters response to computer guess
    response = input("Is computer's guess high (h) or low (l)? ")
    if response == "h":
        high = guess_com - 1
        tries += 1
        guess_com = random.randint(low, high)
        print("\nComputer's guess is:", guess_com)
    else:
        low = guess_com + 1
        tries += 1
        guess_com = random.randint(low, high)
        print("\nComputer's guess is:", guess_com)# Result. Prints out a suitable messageif guess_com == number and tries == 1:
    print("\nWOW!! The computer guessed it in one! Your number was: %d" % number)
   
elif guess_com == number:
    print("\nBad luck %s. The computer guessed the number. Your number was: %d" %(name, number))
    print("The computer needed %d guesses." % tries)else:
    print("\a\nWell done %s! You beat the computer. Your number was: %d" %(name, number))input("\nPress the enter key to exit.")

garrybeck