How to resolve the algorithm Guess the number/With feedback step by step in the Python programming language
How to resolve the algorithm Guess the number/With feedback step by step in the Python programming language
Table of Contents
Problem Statement
Write a game (computer program) that follows the following rules:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Guess the number/With feedback step by step in the Python programming language
This Python code implements a simple number guessing game. Here's a breakdown of how it works:
1. Import the random
module:
import random
This module provides functions for generating random numbers, which will be used to generate the target number that the player has to guess.
2. Define the inclusive range for the target number:
inclusive_range = (1, 100)
This sets the range within which the target number will be generated. In this case, it's between 1 and 100 (inclusive).
3. Print instructions and generate the target number:
print("Guess my target number that is between %i and %i (inclusive).\n"
% inclusive_range)
target = random.randint(*inclusive_range)
This displays instructions for the player and generates a random integer between the specified range, which becomes the target number that the player has to guess.
4. Initialize variables for user input and iteration count:
answer, i = None, 0
answer
will store the player's guess, and i
is used to keep track of the number of guesses the player has made.
5. Guessing loop:
The code enters a loop that continues until the player guesses the correct target number:
while answer != target:
...
6. Get the player's guess:
i += 1
txt = input("Your guess(%i): " % i)
This prompts the player to enter their guess, and the input is stored in txt
.
7. Check if the guess is valid:
try:
answer = int(txt)
except ValueError:
print(" I don't understand your input of '%s' ?" % txt)
continue
The code tries to convert txt
to an integer (int(txt)
). If the conversion fails (e.g., because the user entered a non-numeric character), it prints an error message and continues the loop.
8. Check if the guess is within the range:
if answer < inclusive_range[0] or answer > inclusive_range[1]:
print(" Out of range!")
continue
If the converted guess is outside the specified range, it prints an error message and continues the loop.
9. Check if the guess is correct:
if answer == target:
print(" Ye-Haw!!")
break
If the guess matches the target number, it prints "Ye-Haw!!" and breaks out of the loop.
10. Check if the guess is too low or too high:
if answer < target: print(" Too low.")
if answer > target: print(" Too high.")
If the guess is not correct, it prints "Too low." or "Too high." based on whether the guess is less than or greater than the target number.
11. Print a thank you message after the game ends:
print("\nThanks for playing.")
Once the guessing loop ends, it prints a thank you message to the player.
Source code in the python programming language
import random
inclusive_range = (1, 100)
print("Guess my target number that is between %i and %i (inclusive).\n"
% inclusive_range)
target = random.randint(*inclusive_range)
answer, i = None, 0
while answer != target:
i += 1
txt = input("Your guess(%i): " % i)
try:
answer = int(txt)
except ValueError:
print(" I don't understand your input of '%s' ?" % txt)
continue
if answer < inclusive_range[0] or answer > inclusive_range[1]:
print(" Out of range!")
continue
if answer == target:
print(" Ye-Haw!!")
break
if answer < target: print(" Too low.")
if answer > target: print(" Too high.")
print("\nThanks for playing.")
You may also check:How to resolve the algorithm Averages/Root mean square step by step in the PARI/GP programming language
You may also check:How to resolve the algorithm Truncatable primes step by step in the C# programming language
You may also check:How to resolve the algorithm Flatten a list step by step in the TXR programming language
You may also check:How to resolve the algorithm Least common multiple step by step in the Elixir programming language
You may also check:How to resolve the algorithm Leap year step by step in the Standard ML programming language