-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathnumber_guessing_game.py
More file actions
executable file
·67 lines (55 loc) · 1.96 KB
/
number_guessing_game.py
File metadata and controls
executable file
·67 lines (55 loc) · 1.96 KB
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
import random
import os
import time
import platform
# Maximum number to include in guessing game
# Default is set to include numbers from 1 to 1,000
#
# To use numbers from 1 to 1,000,000 for example, change to: max_num = 1_000_000
#
OK_GREEN = '\033[92m'
FAIL_RED = '\033[91m'
ENDC = '\033[0m'
max_num = 1_000
current_platform = platform.system()
# Scratch file location for tracking winning streak
scratch_file = "./tally.scratch"
correct_responses = ["あたり", "正しい", "よくできました", "すごいね", "正解", "御名答"]
incorrect_responses = ["おしい", "残念", "違う", "不正解", "間違い", "誤り", "違います"]
guess, tally = None, 0
def say(str):
if (current_platform == 'Darwin'):
str = "say -v kyoko " + str
# TODO: And command for window/linux users https://wiki.freepascal.org/espeak
# else:
# str = "espeak"
os.system(str)
return
if os.path.isfile(scratch_file):
with open(scratch_file, "r") as f:
tally = f.read()
tally = int(tally)
os.system("clear")
input("""This program will help you improve your listening comprehension with Japanese numbers.
You will hear a number spoken in Japanese, and then be asked to type in the number.
Type your answer using the traditional western numbers (0-9). Type 'q' to quit.
Make sure your sound is turned on and press ENTER to begin: """)
print()
while True:
num = random.randint(1, max_num)
say(str(num))
guess = input("Type your answer or 'q' to quit: ")
if guess == "q":
with open(scratch_file, "w") as f:
f.write(str(tally))
break
elif int(guess) == num:
say(random.choice(correct_responses))
tally += 1
print(f"{OK_GREEN}correct number: {num} your guess: {guess}{ENDC}\n")
else:
say(random.choice(incorrect_responses))
tally = 0
print(f"{FAIL_RED}correct number: {num} your guess: {guess}{ENDC}\n")
print(f"Streak: {tally}\n")
time.sleep(1)