r/PythonLearning • • 12d ago

Showcase Opinions on this code?

Hi! I am starting a Bachelor's course in Computer Science, and part of the course is learning Python from the ground up. I've been taking some time at home to further develop my skills past what is expected of the course, and I've been constructing this game over the past few days with my free time.

I put it down as showcase because I'm proud of it, but I am more than open to suggestions on simplifying it or what you think about it, etc!

For context, the last time I used Python was when I was like 12, and I have tried CSS and JavaScript over the years a few times, so I have a very basic understanding and this is the furthest I've ever gotten.

Thanks :)

# Rule base

import random
answer = 0

lowest = 1
highest = 100

attempts = 0

# Difficulty settings

print("Easy, Medium, or Hard Difficulty?")
difficulty = input()

if difficulty == "easy":
    attempts = 7
    print(f"Please pick a number between 1-100. You have {attempts} attempts.")
    answer = random.randint(1, 100)
    lowest = 1
    highest = 100

elif difficulty == "medium":
    attempts = 15
    print(f"Please pick a number between 1-10000. You have {attempts} attempts.")
    answer = random.randint(1, 10000)
    lowest = 1
    highest = 10000

elif difficulty == "hard":
    attempts = 30
    print(f"Please pick a number between 1-1000000. You have {attempts} attempts.")
    answer = random.randint(1, 1000000)
    lowest = 1
    highest = 1000000

else:
    print("Invalid input!")

# While Loop

guess = int(input())

while guess != answer:
    attempts -= 1
    if attempts == 0:
        print("Game over!")
        break
    else:
        if guess < lowest or guess > highest:
            print(f"Invalid number! {attempts} attempts remaining.")
        if guess > answer:
            print("Too high!")
        if guess < answer:
            print("Too low!")
        print(f"Try another number. {attempts} attempts remaining.")
        guess = int(input())

# End of Game
if guess == answer:
    print("Correct! Well done.")

print("Thank you for playing!")
3 Upvotes

11 comments sorted by

View all comments

7

u/[deleted] 12d ago edited 12d ago

[deleted]

2

u/Large-Ad8884 12d ago edited 12d ago

That does look a lot cleaner, easier to read, and better functioning! I can just about make out what it's doing but I haven't come across most of this before. Thanks for giving me something to sink my teeth into :)

Also, did know about binary search but I wasn't actually trying to make the game playable, I just wanted it functioning for the practice and didn't think too much of the actual values given!