Files
  • main.py
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
## Guess the number
## The program picks a random number between 1 & 100. It then prompts the user for a guess. After each incorrect guess, the program advises the player to try higher or lower, and tells him/her how many tries are left.
import random

print("I have chosen a random number between 1 & 100.")

number = random.randint(1, 101)

tries = 0
guess = 0

while True:
    guess = int(input("Enter your guess: "))
    
    # if the guess is correct
    if guess == number:
        print("You have correctly guessed the number.")
        break
    
    # if the guess is not correct
    else:
        # record the try
        tries = tries + 1
        # if the user has tries left
        if tries < 7:
            # if the guess was higher than the number
            if guess > number:
                print("Oops! Try lower. You have", 7 - tries, "chances left.")
            # if the guess was lower than the number
            else:
                print("Oops! Try higher. You have", 7 - tries, "chances left.")
        # if the user has run out of tries i.e. tries == 7
        else:
            print("You have run out of guesses. The number was:", number)
            break