|
1 |
| -#!/usr/bin/python |
2 |
| -# -*- coding: utf-8 -*- |
| 1 | +#!/usr/bin/python3 |
| 2 | +from random import randint |
3 | 3 |
|
4 |
| -from random import randint as r_int |
| 4 | +MAX_ = 500 # Specify biggest possible value from random number generation |
5 | 5 |
|
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 |
10 | 15 |
|
| 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") |
11 | 18 |
|
12 | 19 | 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 |
17 | 22 |
|
18 |
| - # if correct |
19 |
| - if usr == n: |
| 23 | + # Correct number! |
| 24 | + if guess == num_to_guess: |
20 | 25 | print("That's a correct guess!\nYou got it!\n")
|
21 | 26 | break
|
22 | 27 |
|
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 |
26 | 31 | else:
|
27 |
| - print("Try a smaller num!") |
| 32 | + print("Try a smaller num!") # Number to big |
28 | 33 |
|
| 34 | + # Number is not in range |
29 | 35 | else:
|
30 |
| - print("Pls read the hint!") |
| 36 | + print("Enter a number in between 0 and ", MAX_) |
31 | 37 |
|
32 |
| - # Continue till guess does not match 'n' |
33 |
| - # the random number |
34 | 38 | print("")
|
35 |
| - continue |
36 | 39 |
|
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