Guessing Game
Page 1 of 1
Guessing Game
[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
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":
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
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum
|
|