Skip to content

Commit 8fde1dd

Browse files
committed
Add try/except to value check
1 parent 37a7676 commit 8fde1dd

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed

guess-game/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ You try to guess a number between 0 and 10
99
* functions
1010
* random numbers
1111
* imports
12-
* `f` strings
12+
* `f` strings
13+
* try/except

guess-game/guess_game_main.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,20 @@ def main():
88
number = randint(0, 10)
99
not_finished = True
1010
while not_finished:
11-
guess = int(input('Guess the number between 0 and 10: '))
12-
returned = game(guess, number)
13-
if returned > 0:
14-
print('Guessed higher')
15-
guessed_times += 1
16-
elif returned < 0:
17-
print('Guessed lower')
18-
guessed_times += 1
19-
else:
20-
not_finished = False
11+
try:
12+
guess = int(input('Guess the number between 0 and 10: '))
13+
returned = game(guess, number)
14+
if returned > 0:
15+
print('Guessed higher')
16+
guessed_times += 1
17+
elif returned < 0:
18+
print('Guessed lower')
19+
guessed_times += 1
20+
else:
21+
not_finished = False
22+
except ValueError:
23+
print(f"Your guess should be a number!")
24+
2125
print(f'End of the game, the number was {number} and you guessed\
2226
{guessed_times} times!')
2327

0 commit comments

Comments
 (0)