I created this python number-guessing game and I have two concerns with it. The code works as intended with no flaws; however, I'm not too fond of the way I wrote it. It is messy, unorganized, and all over the place. There is repeated code such as the while loop that checks for a negative number or a number greater than 30, and the repeating "if number == n".
I wrote it like that because the program would bug if I did not repeat those expressions.
I also want to make it ask the user if they would like to play again; I do not know how.
import random
n = random.randint(1, 31)
choice = input("\nDo you want to play a game? (y/n): ")
if (choice == "y") or (choice == "Y") or (choice == "Yes") or (choice == "yes"):
counter = 0
number = int(input("\nGuess a number between 1 and 30: "))
if (number == n):
print("\nYou guessed correctly!!! Go you!")
counter += 1
while (number < 1) or (number > 30):
number = int(
input("\nThat is an invalid number. Please re-enter a number between 1 and 30: "))
if (number == n):
print("\nYou guessed correctly!!! Go you!")
while number != n:
if (number <= 5) and (n > 5):
number = int(input("\nToo low! Try again: "))
counter += 1
elif (number >= 25) and (n < 25):
number = int(input("\nWay too high! Try again: "))
counter += 1
elif ((n + 5 >= number) and (n - 5 <= number)):
number = int(input("\nYou are so close! Try again: "))
counter += 1
else:
number = int(input("\nWrong! Try again: "))
counter += 1
if (number == n):
print("\nYou guessed correctly!!! Go you!")
while (number < 1) or (number > 30):
number = int(
input("\nThat is an invalid number. Please re-enter a number between 1 and 30: "))
print(f"You took {counter} attempts to guess the number!")
else:
print("\nYour loss....")