You vs. Computer Guess Game (with error handling and Save-into-file high score capability)

Error handling syntax

try:

except:




import random

rNum = random.randint(1, 100)
guesses = 0
while True:
    try:
        gn = int(input("We have chosen a number between 1 and 100. Guess what? "))
        guesses += 1

        if gn < rNum:
                print("Hint: Choose a higer number")

        if gn > rNum:
                print("Hint: Choose a lower number")

        if gn == rNum:
                print(f"You guessed it right. Your number was {gn}")
                print(f"Our chosen number was {rNum}")
                print(f"You geussed it right in {guesses} guesses")
                break

    except:
        print("invalid input. Plz enter only whole numbers!")

with open("hi-score2.txt", "r") as f:
        hiscore = int(f.read())
if guesses < hiscore:
    with open("hi-score2.txt", "w") as f:
        f.write(str(guesses))




Comments

Popular posts from this blog

Quotation marks to wrap an element in HTML

The Basic Structure of a Full-Stack Web App

Unlocking Web Design: A Guide to Mastering CSS Layout Modes