• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
PythonForBeginners.com

PythonForBeginners.com

Learn By Example

  • Home
  • Learn Python
    • Python Tutorial
  • Categories
    • Basics
    • Lists
    • Dictionary
    • Code Snippets
    • Comments
    • Modules
    • API
    • Beautiful Soup
    • Cheatsheet
    • Games
    • Loops
  • Python Courses
    • Python 3 For Beginners
You are here: Home / Code Snippets / Python : Guessing Game part 2

Python : Guessing Game part 2

Author: PFB Staff Writer
Last Updated: July 15, 2021

Overview

This small program extends the previous guessing game I wrote about in this
: post “Python Guessing Game”.

Guessing Game

In this game we will add a counter for how many guesses the user can have.

The counter is initial set to zero.

The while loop will run as long as the guesses are less to 5.

If the user guess the right number before that, the script will break and
present the user with how many guesses it took to guess the right number.

The variables in this script can be changed to anything.

I will break up the program in chunks to make it easier to read

First we import the Random module

import random

Then we give the “number” variable a random number between 1 and 99.

number = random.randint(1, 99)

Set the guesses variable to 0, which will count the guesses

guesses = 0

As long as the guesses are less than 5, ask the user to guess a number.

Then increase the guesses counter with 1.

Print out a message to the user the number of guesses.

while guesses < 5:
    guess = int(raw_input("Enter an integer from 1 to 99: "))
    guesses +=1
    print "this is your %d guess" %guesses

Check if the guess is lower, higher or equal to our random number and print
a message of the result.

If the guess is the same as our number, exit the program.

    if guess < number:
        print "guess is low"
    elif guess > number:
        print "guess is high"
    elif guess == number:
        break

Print out the how many guesses the user had.

if guess == number:
    guesses = str(guesses)
    print "You guess it in : ", guesses + " guesses"

If the user couldn’t guess the right number in 5 guesses, print out what the
secret number was.

if guess != number:
    number = str(number)
    print "The secret number was",  number

I hope you had fun with this guessing game.

Related

Recommended Python Training

Course: Python 3 For Beginners

Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.

Enroll Now

Filed Under: Code Snippets, Games, Scripts Author: PFB Staff Writer

More Python Topics

API Argv Basics Beautiful Soup Cheatsheet Code Code Snippets Command Line Comments Concatenation crawler Data Structures Data Types deque Development Dictionary Dictionary Data Structure In Python Error Handling Exceptions Filehandling Files Functions Games GUI Json Lists Loops Mechanzie Modules Modules In Python Mysql OS pip Pyspark Python Python On The Web Python Strings Queue Requests Scraping Scripts Split Strings System & OS urllib2

Primary Sidebar

Menu

  • Basics
  • Cheatsheet
  • Code Snippets
  • Development
  • Dictionary
  • Error Handling
  • Lists
  • Loops
  • Modules
  • Scripts
  • Strings
  • System & OS
  • Web

Get Our Free Guide To Learning Python

Most Popular Content

  • Reading and Writing Files in Python
  • Python Dictionary – How To Create Dictionaries In Python
  • How to use Split in Python
  • Python String Concatenation and Formatting
  • List Comprehension in Python
  • How to Use sys.argv in Python?
  • How to use comments in Python
  • Try and Except in Python

Recent Posts

  • Count Rows With Null Values in PySpark
  • PySpark OrderBy One or Multiple Columns
  • Select Rows with Null values in PySpark
  • PySpark Count Distinct Values in One or Multiple Columns
  • PySpark Filter Rows in a DataFrame by Condition

Copyright © 2012–2025 · PythonForBeginners.com

  • Home
  • Contact Us
  • Privacy Policy
  • Write For Us