Guessing Game
This script is an interactive guessing game, which will ask the user to guess a number between 1 and 99.
We are using the random module with the randint function to get a random number. The script also contains a while loop, which make the script run until the user guess the right number.
If you read my previous post about conditional statements in Python, you will also recognize the if, elif and else statements.
import random
n = random.randint(1, 99)
guess = int(raw_input("Enter an integer from 1 to 99: "))
while n != "guess":
print
if guess < n:
print "guess is low"
guess = int(raw_input("Enter an integer from 1 to 99: "))
elif guess > n:
print "guess is high"
guess = int(raw_input("Enter an integer from 1 to 99: "))
else:
print "you guessed it!"
break
print
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.