Python

John Saari
PRO
John Saari
Pro Student 8,952 Points

My number game reversal

I created a version where the USER picks the range (min/max), number of attempts and secret number. The COMPUTER then attempts to pick the number. I borrowed several steps from the letter game, but am having trouble resetting the rules if the USER wants to play again. Thanks!

import random
import os
import sys
rules = []

def clear():
    if os.name == 'nt':
        os.system('cls')
    else:
        os.system('clear')

def set_rules():
    min_num = int(input("What is the beginning of the range: \n>"))
    max_num = int(input("what is the end of the range: \n>"))
    attempt_num = int(input("How many attempts does the computer get? \n>"))
    rules.append(min_num)
    rules.append(max_num)
    rules.append(attempt_num)

def draw(guesses):
    clear()

    print("The computer it going to try and guess an number between {} and {}:".format(rules[0], rules[1]))
    print('Strikes: {}/{}'.format(len(guesses),rules[2]))
    print('')

    for num in guesses:
        print(num, end=' ')
    print('\n\n')


def play(done):
    clear()
    secret_num = int(input("Pick a number between {} and {} to see if the computer can guess it in {} tries: \n>".format(rules[0], rules[1], rules[2])))
    guesses = []

    while True:
        draw(guesses)
        guess = get_guess(guesses)

        if guess == secret_num:
            found = True
            if found:
                print("The computer got it!")
                print("The secret number you entered was {}".format(secret_num))
                done = True
        else:
            guesses.append(guess)
            if len(guesses) == rules[2]:
                draw(guesses)
                print("The computer didn't guess it!")
                print("The secret number you entered was {}".format(secret_num))
                done = True

        if done:
            play_again = input("Play again? Y/n ").lower()
            if play_again != 'n':
                return play(done=False)
            else:
                sys.exit()


def get_guess(guesses):
    while True:
        guess = random.randint(rules[0],rules[1])
        if guess in guesses:
            continue
        else:
            return guess

def welcome():
    print('Welcome to Number Guess!')

    start = input("Press enter/return to start or Q to quit ").lower()
    if start == 'q':
        print("Bye!")
        sys.exit()
    else:
        return True


done = False

while True:
    clear()
    welcome()
    set_rules()
    play(done)

1 Answer

Steven Parker
Steven Parker
105,281 Points

Did you just forget to clear the rules and then call "set_rules"?

        if done:
            play_again = input("Play again? Y/n ").lower()
            if play_again != 'n':
                del rules[:]               # clear the old rules
                set_rules()                # then set them again
                return play(done=False)

Another option would be to have "play" call "set_rules" itself, and have "set_rules" start by clearing the list.

John Saari
John Saari
Pro Student 8,952 Points

I tried the change above and got an UnboundLocalError (referenced before assignment. I also tried several different scenarios to clear the rules list and call set_rules as part of play, but would also get a NameError where the variable rules or secret_num isn't defined for the play(done)

Steven Parker
Steven Parker
105,281 Points

Oops, I didn't test it and accidentally created a new variable with a different scope. I revised my answer so the code does what I originally intended.

John Saari
John Saari
Pro Student 8,952 Points

That was it! I'll have to learn a little more about scoping to figure this one out!