Skip to content

Commit 77ca7c9

Browse files
authored
Merge pull request kal179#11 from SillySam/patch-1
Added comments and error handling
2 parents fdaab72 + 56d36e3 commit 77ca7c9

File tree

1 file changed

+35
-25
lines changed

1 file changed

+35
-25
lines changed

shell_games/number_guessing_game.py

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,50 @@
1-
#!/usr/bin/python
2-
# -*- coding: utf-8 -*-
1+
#!/usr/bin/python3
2+
from random import randint
33

4-
from random import randint as r_int
4+
MAX_ = 500 # Specify biggest possible value from random number generation
55

6-
# generates a random number
7-
# in inclusive list from 0 to 10
8-
n = r_int(0, 10)
9-
print("Hint: number is between 0 to 10 including 0 and 10\n")
6+
# Asks user for a guess and returns this when successful
7+
def get_guess():
8+
while True: # Run forever, until broken
9+
try: # Try the following, if fails, run the except statment
10+
user_input = int(input("Guess a number: ")) # Get user input and covert it to an int (number)
11+
break # If the input can be converted, break the loop
12+
except ValueError: # This runs if the input could not be converted - text was entered
13+
print("Enter a number!\n") # Tell them to enter a number, then run loop again
14+
return user_input # Return value when loop broken
1015

16+
num_to_guess = randint(0, MAX_) # Generates a random number between 0 and value of MAX_
17+
print("Welcome to my random number guessing game!\nGuess a number between 0 and ", MAX_, "\n")
1118

1219
while True:
13-
usr = int(raw_input("Guess a number: "))
14-
15-
# Make sure user got the hint
16-
if usr <= 10 and usr >= 0:
20+
guess = get_guess()
21+
if guess >= 0 and guess <= MAX_: # Check if guess between 0 and max
1722

18-
# if correct
19-
if usr == n:
23+
# Correct number!
24+
if guess == num_to_guess:
2025
print("That's a correct guess!\nYou got it!\n")
2126
break
2227

23-
# brings user closer to answer
24-
elif usr < n:
25-
print("Try a bigger num!")
28+
# Incorrect - give hint
29+
elif guess < num_to_guess:
30+
print("Try a bigger num!") # Number to small
2631
else:
27-
print("Try a smaller num!")
32+
print("Try a smaller num!") # Number to big
2833

34+
# Number is not in range
2935
else:
30-
print("Pls read the hint!")
36+
print("Enter a number in between 0 and ", MAX_)
3137

32-
# Continue till guess does not match 'n'
33-
# the random number
3438
print("")
35-
continue
3639

37-
# Is an infinite loop if usr keeps guessing wrong
38-
# an extra loop can be added to play game as long as user wants
39-
# or can play number of times the user entered!
40-
# Also can be made more harder to play
40+
'''
41+
42+
*Challenge*
43+
44+
1) Can you give the user the option to Play Again?
45+
- if yes, the game will reset - including the random number
46+
47+
2) Try count the number of guesses and give a score to the user when they guess the number
48+
- the lower the guesses, the higher the score
49+
50+
'''

0 commit comments

Comments
 (0)