MCPcopy Create free account
hub / github.com/geekcomputers/Python / guessing_game

Function guessing_game

Guessing_Game.py:5–41  ·  view source on GitHub ↗
(GUESS_RANGE, GUESS_LIMIT)

Source from the content-addressed store, hash-verified

3
4
5def guessing_game(GUESS_RANGE, GUESS_LIMIT):
6 # Set the initial values.
7 RANDOM = randint(1, GUESS_RANGE)
8 GUESS = int(input("What is your guess? "))
9 ATTEMPTS_ALLOWED = GUESS_LIMIT
10 done = False
11
12 # Validate the inputted guess.
13 GUESS = InputValidation(GUESS, GUESS_RANGE)
14
15 # Now we have a valid guess.
16 while GUESS_LIMIT > 0 and not done:
17 GUESS_LIMIT -= 1 # Take one guess = lose one chance
18 if GUESS_LIMIT > 0:
19 if GUESS < RANDOM:
20 print(f"It should be higher than {GUESS}.")
21 elif GUESS > RANDOM:
22 print(f"It should be lower than {GUESS}.")
23 else:
24 ATTEMPTS_TOOK = ATTEMPTS_ALLOWED - GUESS_LIMIT
25 print(f"You nailed it! And it only took you {ATTEMPTS_TOOK} attempts.")
26 done = True
27 if GUESS_LIMIT > 0 and not done:
28 print(f"You still have {GUESS_LIMIT} chances left.\n")
29 GUESS = int(input("Try a new guess: "))
30 # Another input validation loop.
31 GUESS = InputValidation(GUESS, GUESS_RANGE)
32 elif GUESS_LIMIT == 0 and not done: # Last chance to guess
33 if GUESS == RANDOM:
34 print(
35 f"You nailed it! However, it took you all the {ATTEMPTS_ALLOWED} attempts."
36 )
37 else:
38 print(
39 f"GAME OVER! It took you more than {ATTEMPTS_ALLOWED} attempts. "
40 f"The correct number is {RANDOM}."
41 )
42
43
44def InputValidation(GUESS, GUESS_RANGE):

Callers 3

easyFunction · 0.85
mediumFunction · 0.85
hardFunction · 0.85

Calls 1

InputValidationFunction · 0.85

Tested by

no test coverage detected