Guessing Game

Go down

Guessing Game

Post by Admin on Mon Oct 16, 2017 6:28 pm

[b]Problem:[/b]
Writing a script which will ask the user to guess a number between 1 and 99.

[b]Solution:[/b]
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.

[b]Script:[/b]
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
avatar
Admin
Admin

Posts : 25
Join date : 2017-08-17
Location : Torrance CA

View user profile http://southbayrobot.com

Back to top Go down

Back to top


 
Permissions in this forum:
You cannot reply to topics in this forum