Skip to content

Instantly share code, notes, and snippets.

@rostyq
Created July 28, 2017 18:12
Show Gist options
  • Save rostyq/283d85cbffcf684d909e4b054516cad9 to your computer and use it in GitHub Desktop.
Save rostyq/283d85cbffcf684d909e4b054516cad9 to your computer and use it in GitHub Desktop.
import random
def new():
global num, count
num = random.randint(1, 9)
count = 0
too = [8, 7, 6, 5]
close = [4, 3, 2, 1]
new()
while True:
user = input("Guess the number: ")
if user == 'exit': break
try:
user = int(user)
except:
print("Something wrong. Try again.")
continue
count += 1
d = user - num
if d == 0:
print("Exactly right! You won!\nCount of guessing: "+str(count), end='.\n')
user = input("One more game? -> ")
if user == 'y':
new()
continue
else:
break
elif d > 0:
if d in too:
print("Too high!")
elif d in close:
print("High!")
elif d < 0:
d = abs(d)
if d in too:
print("Too low!")
elif d in close:
print("Low!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment