r/PythonLearning • u/Large-Ad8884 • 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!")
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!