Skip to content

Commit 31f63d5

Browse files
committed
exercise 9
1 parent e28e6cd commit 31f63d5

File tree

2 files changed

+142
-0
lines changed

2 files changed

+142
-0
lines changed

exericises/exercises_9.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
'''
5+
Exercise
6+
7+
Generate a random number between 1 and 9 (including 1 and 9). Ask the user to guess the number, then tell them whether they guessed too low, too high, or exactly right. (Hint: remember to use the user input lessons from the very first exercise)
8+
9+
Extras:
10+
11+
Keep the game going until the user types “exit”
12+
Keep track of how many guesses the user has taken, and when the game ends, print this out.
13+
'''
14+
15+
16+
from __future__ import print_function
17+
import random
18+
19+
try:
20+
input = raw_input
21+
except NameError:
22+
pass
23+
24+
25+
'''
26+
I uses a compatible way to write the code this time
27+
The following code can run both in python 2 and python 3
28+
'''
29+
30+
num = random.randint(1, 9)
31+
32+
count = 0
33+
34+
def guess_count_message(count):
35+
if count==1:
36+
return 'You guessed 1 time'
37+
else:
38+
return 'You guessed {0} times'.format(count)
39+
40+
def get_input_number():
41+
val = input('Input a number between 1 and 9 to guess, or input <exit> to quit: ')
42+
if val.lower()=='exit':
43+
val = 0
44+
else:
45+
try:
46+
val = int(val)
47+
except ValueError:
48+
val = -1
49+
50+
return val
51+
52+
while True:
53+
val = get_input_number()
54+
# print(val)
55+
exit_signal = False
56+
57+
while val<=0:
58+
if val==0:
59+
exit_signal = True
60+
break
61+
else:
62+
val = get_input_number()
63+
64+
if exit_signal:
65+
break
66+
67+
68+
count+=1
69+
70+
if val == num:
71+
print('You guessed exactly right')
72+
print(guess_count_message(count))
73+
break
74+
75+
elif val>num:
76+
print('You guessed higher')
77+
else:
78+
print('You guessed lower')

py3_solution/exercises_9.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
'''
5+
Exercise
6+
7+
Generate a random number between 1 and 9 (including 1 and 9). Ask the user to guess the number, then tell them whether they guessed too low, too high, or exactly right. (Hint: remember to use the user input lessons from the very first exercise)
8+
9+
Extras:
10+
11+
Keep the game going until the user types “exit”
12+
Keep track of how many guesses the user has taken, and when the game ends, print this out.
13+
'''
14+
15+
16+
from __future__ import print_function
17+
import random
18+
19+
try:
20+
input = raw_input
21+
except NameError:
22+
pass
23+
24+
25+
'''
26+
I uses a compatible way to write the code this time
27+
The following code can run both in python 2 and python 3
28+
'''
29+
30+
num = random.randint(1, 9)
31+
32+
count = 0
33+
34+
def guess_count_message(count):
35+
if count==1:
36+
return 'You guessed 1 time'
37+
else:
38+
return 'You guessed {0} times'.format(count)
39+
40+
while True:
41+
val = input('Guess the random number between 1 and 9, or input <exit> to quit: ')
42+
if val.lower()=='exit':
43+
print('exit')
44+
break
45+
46+
while True:
47+
try:
48+
val = int(val)
49+
break
50+
except ValueError:
51+
print('You need to input a number')
52+
val = input('Guess the random number between 1 and 9, or input <exit> to quit: ')
53+
54+
count+=1
55+
56+
if val == num:
57+
print('You guessed exactly right')
58+
print(guess_count_message(count))
59+
break
60+
61+
elif val>num:
62+
print('You guessed higher')
63+
else:
64+
print('You guessed lower')

0 commit comments

Comments
 (0)