Skip to content

Instantly share code, notes, and snippets.

@gsinclair
Created February 18, 2021 01:19
Show Gist options
  • Save gsinclair/67053c5d60a3857ddf9e8504963f3962 to your computer and use it in GitHub Desktop.
Save gsinclair/67053c5d60a3857ddf9e8504963f3962 to your computer and use it in GitHub Desktop.
Computer guessing game
import random
UPPER_LIMIT = 1000000
target = random.randint(1, UPPER_LIMIT)
print("I've chosen a number between 1 and", UPPER_LIMIT)
num_guesses = 0
while True:
print()
try:
guess = int(input("Input your guess: "))
num_guesses += 1
except ValueError:
print("Can't interpret your input; try again")
continue
if guess > target:
print("Lower")
elif guess < target:
print("Higher")
else:
print("CORRECT!!!")
print()
print("It took you", num_guesses, "guesses to get the correct number")
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment