How to make this change the number of guesses depending on prev performance each replay?

30 Views Asked by At

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

1

There are 1 best solutions below

1
Xiidref On
  1. I would add a parameter to the run function that give the max number of possible guess.

  2. Instead of comparing the number of guess done by the user in the loop to 10 you can compare it to the parameter.

  3. When you call run again for replaying a game, you can pass guesses - 1 to make the game harder each time (make sure to keep a minimum of 1 guess).

  4. Change the initial call to run to pass the initial amount of guess you want the first time.

def run(maxAllowedGuess): #1
    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 == maxAllowedGuess: #2
            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(max(guesses - 1, 1)) # 3
        elif goagain.upper() == 'N':
            print('seeya then')
            print(a)

run(10) #4