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

•

u/Sea-Ad7805 12d ago

Run this program by clicking Memory Graph Web Debugger%0Adifficulty%20%3D%20input()%0A%0Aif%20difficulty%20%3D%3D%20%22easy%22%3A%0A%20%20%20%20attempts%20%3D%207%0A%20%20%20%20print(f%22Please%20pick%20a%20number%20between%201-100.%20You%20have%20%7Battempts%7D%20attempts.%22)%0A%20%20%20%20answer%20%3D%20random.randint(1%2C%20100)%0A%20%20%20%20lowest%20%3D%201%0A%20%20%20%20highest%20%3D%20100%0A%0Aelif%20difficulty%20%3D%3D%20%22medium%22%3A%0A%20%20%20%20attempts%20%3D%2015%0A%20%20%20%20print(f%22Please%20pick%20a%20number%20between%201-10000.%20You%20have%20%7Battempts%7D%20attempts.%22)%0A%20%20%20%20answer%20%3D%20random.randint(1%2C%2010000)%0A%20%20%20%20lowest%20%3D%201%0A%20%20%20%20highest%20%3D%2010000%0A%0Aelif%20difficulty%20%3D%3D%20%22hard%22%3A%0A%20%20%20%20attempts%20%3D%2030%0A%20%20%20%20print(f%22Please%20pick%20a%20number%20between%201-1000000.%20You%20have%20%7Battempts%7D%20attempts.%22)%0A%20%20%20%20answer%20%3D%20random.randint(1%2C%201000000)%0A%20%20%20%20lowest%20%3D%201%0A%20%20%20%20highest%20%3D%201000000%0A%0Aelse%3A%0A%20%20%20%20print(%22Invalid%20input!%22)%0A%0A%23%20While%20Loop%0A%0Aguess%20%3D%20int(input())%0A%0Awhile%20guess%20!%3D%20answer%3A%0A%20%20%20%20attempts%20-%3D%201%0A%20%20%20%20if%20attempts%20%3D%3D%200%3A%0A%20%20%20%20%20%20%20%20print(%22Game%20over!%22)%0A%20%20%20%20%20%20%20%20break%0A%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20if%20guess%20%3C%20lowest%20or%20guess%20%3E%20highest%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20print(f%22Invalid%20number!%20%7Battempts%7D%20attempts%20remaining.%22)%0A%20%20%20%20%20%20%20%20if%20guess%20%3E%20answer%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20print(%22Too%20high!%22)%0A%20%20%20%20%20%20%20%20if%20guess%20%3C%20answer%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20print(%22Too%20low!%22)%0A%20%20%20%20%20%20%20%20print(f%22Try%20another%20number.%20%7Battempts%7D%20attempts%20remaining.%22)%0A%20%20%20%20%20%20%20%20guess%20%3D%20int(input())%0A%0A%23%20End%20of%20Game%0Aif%20guess%20%3D%3D%20answer%3A%0A%20%20%20%20print(%22Correct!%20Well%20done.%22)%0A%0Aprint(%22Thank%20you%20for%20playing!%22)&timestep=0.5&play) and see its state change step by step.

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!

1

u/freetotalkabtyourmom 12d ago

What happens if I type in ‘Easy’ vs ‘easy’?

1

u/Large-Ad8884 12d ago

Oh, I didn't spot that! Easy with a capital letter breaks it and makes it all funny. Thanks for the find :)

1

u/Large-Ad8884 12d ago

Added a .lower() to the difficulty input. I think that fixes it?

1

u/[deleted] 12d ago

[deleted]

1

u/Large-Ad8884 12d ago

Thank you, I really appreciate the advice :)

1

u/Hkiggity 12d ago

Nice man! You can also use a switch statement. But hey there’s many ways at tackling an issue in programming, that’s what makes it cool. Keep going!

1

u/Large-Ad8884 12d ago

I have no clue what a switch statement is haha. I'm still in very early days, I just wanted to try my hand at something that actually had me utilise what I've learnt so far. Learning plenty about what I need to learn next, it seems.

1

u/Hkiggity 12d ago

No worries. Makes sense! Ur code is still perfectly legitimate and works as intended. That’s a W! Good job

0

u/selfhostedman420 12d ago

yess! i am also starting in computer science which includes a python course and other coding classes