code:
import random
import timeit
def run():
lower_bound = int(input('Enter lower bound'))
upper_bound = int(input('Enter upper bound'))
number = random.randint(lower_bound,upper_bound)
a = timeit.default_timer()
guess = int(input(f'Guess a number between {lower_bound} and {upper_bound}'))
guesses = 1
while guess != number:
print('wrong')
guesses += 1
if guess > number:
print('too high')
elif guess < number:
print('too low')
if guesses == 10:
break
if a >= 300000:
break
guess = int(input(f'Guess a number between {lower_bound} and {upper_bound}'))
if guesses == 10:
print('you ran out of guesses')
elif a >= 300000:
print('you ran out of time')
print(a)
else:
print(f'Good job, ya got it! but in {guesses} guesses')
print(a)
goagain = input('Do you wish to play again?(Y/N) ')
if goagain.upper() == 'Y':
run()
elif goagain.upper() == 'N':
print('seeya then')
print(a)
run()
This is meant to be an adaptive number guessing game that gets harder or easier depending on previous performance each replay, also has time integrated but I don't really want to mess with that I've tried to use a separate function, but I couldn't run it due to trying to ref it before assignment(same with variables) I need something like a variable that changes the total number of guesses based off of how the user performed the last run
I would add a parameter to the run function that give the max number of possible guess.
Instead of comparing the number of guess done by the user in the loop to 10 you can compare it to the parameter.
When you call
runagain for replaying a game, you can passguesses - 1to make the game harder each time (make sure to keep a minimum of 1 guess).Change the initial call to
runto pass the initial amount of guess you want the first time.