-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathnumguess.py
81 lines (62 loc) · 1.75 KB
/
numguess.py
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
from __future__ import print_function
import sys, random, math
print("Welcome to NumGuess Python terminal version!\n")
print("Enter your name: ", end="", flush=True)
name = sys.stdin.readline().strip()
if name == "":
name = "Player"
print("\nWelcome " + name + ", enter limit: ", end="", flush=True)
limit = sys.stdin.readline().strip()
try:
limit = int(limit)
if limit < 10:
limit = 10
except:
limit = 10
max_tries = math.floor(math.log(limit, 2)) + 1
while True:
tries = 0
number = random.randint(1, limit)
print("\nGuess my number between 1 and " + str(limit) + "!\n")
while True:
print("Guess: ", end="", flush=True)
guess = sys.stdin.readline().strip()
try:
guess = int(guess)
if guess > limit or guess < 1:
print("\bOut of range.")
else:
if guess < number:
tries = tries + 1
print("\bToo low!")
elif guess > number:
tries = tries + 1
print("\bToo high!")
else:
tries = tries + 1
break
except:
print("\bThat's just plain wrong.")
tries_word = "try"
if tries > 1:
tries_word = "tries"
print("\nWell done " + name + ", you guessed my number from " + str(tries) + " " + tries_word + "!")
custom_message = ""
if tries == 1:
custom_message = "You're one lucky bastard!"
elif tries < max_tries:
custom_message = "You are the master of this game!"
elif tries == max_tries:
custom_message = "You are a machine!"
elif tries <= max_tries * 1.1:
custom_message = "Very good result!"
elif tries <= limit:
custom_message = "Try harder, you can do better!"
else:
custom_message = "I find your lack of skill disturbing!"
print(custom_message)
print("Play again [y/N]? ", end="", flush=True)
again = sys.stdin.readline().strip()
if again.upper() != 'Y':
break
print("\nOkay, bye.")