Task:
Create a program where the computer randomly selects an integer between 1 and 10. The user then has to guess which number it is. If the guess is wrong, the program should keep asking until the user guesses correctly.
This is the final code. Below is the documentation of this code.
"""
Uppgift 5 Python
Skapa ett program där datorn slumpar fram ett heltal mellan 1 och 10.
Användaren får sedan gissa vilket tal det är. Om gissningen är fel,
ska programmet fortsätta fråga tills användaren gissar rätt.
"""
player_name = input("Type your name")
print(f'Hello {player_name}! Lets play a number guessing game.')
""" Ändra programmet för att räkna antalet gissningar"""
import random
max_tries = 3
random_number = random.randint(1, 10)
guess = int(input('Guess your number between 1 and 10: '))
tries = 0 # tries start count from 0.
while guess != random_number:
if guess > random_number:
print("guess lower")
elif guess < random_number:
print("guess higher")
tries += 1 # For every guess, increment by 1
if tries == max_tries: # use max_tries variable
print(f'Game Over! Answer is {random_number}') # if tries == max_tries, gameover
break # Then use break statement here!
guess = int(input('Guess your number between 1 and 10: '))
if guess == random_number:
print ("Congratulations! You guessed the number!")
Step 1: Use the random Python package and use the randint function. It generates a random integer (secret number).
Step 2: Make random_number as variable for the secret number from Python.
In random.randint, use randint built-in function and (1, 10) as my parameter. The code will now generate numbers between 1 to 10, without showing. Make an input for the user to guess. Don’t forget to typecast the guess input into an integer.
Step 3: Next, use the while loop to continue “looping” until the user guesses the right answer.
The logic is: Continue looping while guess != random_number. (Comparative Operator !=) This guess may not be the same value as the random number. The game will keep on looping until the right answer is guessed.
Stop looping if guess == random_number. (Comparative Operator ==) This guess is the same value as the random number. Add the if statement in the same code block (indention) as the while statement. If guess == random_number, the while loop breaks and the user wins.
Step 4: Next, Python tells hints if the guess is too high or too low. Use the if statement within the while loop. If the first condition = False, then try the second condition.
First if condition: If guess is more than > random_number, within the if statement, print “guess lower.”
Second condition: Elif is used if the previous condition was not True. This is an alternative condition, which is to guess higher. Elif condition should be the same code block with the if statement. Print under the code block.
Step 5: Run the code, see the output and do some debugging.
Output: Fix the Value Error by casting int to the (1, 10) parameter.
Error: The while loop does not end because the user can only guess once. Also, the if and elif statement did not work here. Fix this by giving the user another chance to guess.
Add another code to make another guess inside the while loop.
Output: Problem here. Delete (int(1, 10).