This program is supposed to compare the user's input to the randomly created integer stored in the 'n' variable. Also, once achieved, it should return the number of attempts taken by the user. For some reason, the 'attempts' variable is not being increased after each iteration. I suppose this problem is related to the position of my while loop.
import random
import time
import sys
def number_guess():
n = random.randint(1,75)
attempts = 0
print(n)
guess = int(input('Enter an integer from 1 to 75: '))
if guess == n:
print ('You Guessed It!')
print ('You guessed the correct number in %s attempts!!!' % (attempts)) #This line always shows '0 attempts'
time.sleep(5)
sys.exit()
else:
while guess != n:
if guess < 1 or guess > 75:
print ('Remember, the integer should be from 1 to 75')
attempts += 1
number_guess()
else:
print ('You Missed It')
attempts += 1
number_guess()
number_guess()
As you can see, I'm quite new to programming. Any corrections will be greatly appreciated. Thanks!