I am trying to create a simple Python project. It's a minigame about guessing a number.
It's a game where the computer will pick a random number from 1 to 10.
- The player needs to guess it correctly.
- Every wrong attempt will give you a clue.
- 3 clues and the player will lose.
I've built the code and it works just fine. Here's the code :
total = 0
guessme = 0
def GuessNum(guess):
global total
global guessme
prime = [2, 3, 5, 7]
if total == 0:
randnum = random.randint(1,10)
guessme = randnum
total += 1
if guess == guessme:
print("You are correct!")
guessme = 0
total = 0
else:
if total == 1:
if guessme % 2 == 0:
print("The number is even")
else:
print("The number is odd")
elif total == 2:
if guessme > 5:
print("The number is greater than 5")
else:
print("The number is smaller than 5")
elif total == 3:
if guessme in prime:
print("The number is prime")
else:
print("The number is not a prime")
else:
guessme = 0
total = 0
print("You failed to guess the number.")
Are there ways to make this code much more efficient or shorter? I am still new at programming so I would love to learn new things.
The number is smaller than 5
when the number is 5 is, hm, tricky. \$\endgroup\$ – greybeard Dec 1 '20 at 20:51