-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathguess_number.py
65 lines (52 loc) · 1.86 KB
/
guess_number.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
# This game is to guess a number from 1 to x
# Created by Abdullah Al-Yamani
import random
def guess(x):
random_number = random.randint(1, x)
guess = 0
while guess != random_number:
guess = int(input(f'\n\033[;32;mGuess a number between 1 and {x}: '))
if guess < random_number:
print('\033[;31;mSorry, guess again. Too low.')
elif guess > random_number:
print('\033[;31;mSorry, guess again. Too high.')
print(f'\n\033[;36;mYay, congrats. You have guessed the number {random_number} correctly!!')
def computer_guess(x):
low = 1
high = x
feedback = ''
print(f"""\n\n\033[;36;mChoose a number in your mind between 1 and {x}.
If the computer guesses the number you chose, then you will be press (c).\n""")
while feedback != 'c':
if low != high:
guess = random.randint(low, high)
else:
guess = low # could also be high b/c low = high
feedback = input(f'\033[;32;mIs {guess} too high (H), too low (L), or correct (C)?? ').lower()
if feedback == 'h':
high = guess - 1
elif feedback == 'l':
low = guess + 1
print(f'\n\033[;36;mYay! The computer guessed your number, {guess}, correctly!')
print("\033[;36;mThis Game To Guess a Number From 1 to x\n")
try:
high_num = int(input("\033[;32;mEnter Number (x): "))
except:
print("\n\033[;31;mPlease Enter Only Numbers!!")
high_num = int(input("\033[;32;mEnter Number: "))
print("""\033[;39;m
Do you want to
[1] Guess the Number
[2] Computer Guesses the Number
""")
num = input("\033[;32;mEnter 1 or 2 : ")
while True:
if num == '1':
guess(high_num)
break
elif num == '2':
computer_guess(high_num)
break
else:
print("\n\033[;31;mPlease Enter 1 or 2")
num = input("\033[;32;mEnter 1 or 2 : ")