-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinary search guessing game.py
More file actions
30 lines (24 loc) · 962 Bytes
/
Binary search guessing game.py
File metadata and controls
30 lines (24 loc) · 962 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# Guess the number
#This program generates a random number between 1 and 64 and
#the player has to guess it.
#The computer tells the player each time if their guess is too low or too high
#or if they have guessed it correctly
import random
print("This is a simple guessing game.")
print("I am going to think of a number between 1 and 64, and you have to guess it.")
print("I will tell you whether your guess is too high, too low or correct each time.")
print("You should be able to guess it in 6 guesses or less.")
#initialise variables
myNumber = random.randint(1,64)
guess = int(input("Please type your guess: "))
numberOfGuesses = 1
#main loop
while guess != myNumber:
if guess > myNumber:
print("Your guess is too high")
else:
print("Your guess is too low")
guess = int(input("Guess again: "))
numberOfGuesses = numberOfGuesses + 1
print("Correct! You took",numberOfGuesses,"guesses.\n")
input("\nPress Enter to exit.")