Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Session 03 - Python for Forensics

The code here is to support the Session 03 lecture.

guessingGame.py

import random

def main():
    print("Hello! What is your name?")
    userName = input()

    print(f"Well, {userName}, I am thinking of a number between 1 and 20!")

    secretNumber = random.randint(1, 20)
    attempts = 0

    while attempts < 6:
        print("Take a guess:")
        userGuess = int(input())

        attempts += 1

        if userGuess < secretNumber:
            print("Too Low!!")
        elif userGuess > secretNumber:
            print("Too High!!")
        else:
            print(f"Good Job! It took you {attempts} attempts to guess the number.")
            break

    if userGuess != secretNumber:
        print(f"Sorry, {userName}. The number I was thinking of was {secretNumber}.")

if __name__ == "__main__":
    main()
[ Last updated: Tuesday 13 February 2024 @ 11:22:49 +0000 // branch: master // commit: bd41815 ]