Simple number guessing game implemented by python

  • 2020-05-05 11:27:03
  • OfStack

This article illustrates a simple number guessing game implemented by python. Share with you for your reference. The details are as follows:

Given a number between 1 and 99, let the user guess the number. When the user guesses incorrectly, it will prompt the user to guess whether the number is too large or too small. Until the user guesses the number correctly, the fewer times the user guesses the number correctly, the better the result.


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

I hope this article has been helpful to your Python programming.


Related articles: