What is wrong with this Bagels program?

Viewed 17

Im a beginner programer and i need some help with the following: I have written a Bagels program and it isnt working: I have no idea how to fix it. Please can someone help me!!! It keeps on saying the secretnumber is not defined, and i defined it near the beginning of name: Its based on some code(I didnt actually look at the code, just the example run, as i wanted a challenge)from Invent your own computer game 2nd edition. This is the program:

import random

def secretnum():
    numbers = list('0123456789')
    random.shuffle(numbers)
    secretnumber = ''
    numdigits = 3
    for  i in range(numdigits):
        secretnumber += str(numbers[i])
    return secretnumber

def ask():
    print("Do you want to play again?(y/n): ")
    ans = input()
    if ans == "y":
        print("Ok then!")
        main()
    elif ans == "n":
        print("OOF")
        exit()
    else:
        print("...Invalid...")
        ask()

def getclues(guess, secretnumb):
    if guess == secretnumber:
        return 'You ot it!!!'
    clues = []

    for i in range(len(guess)):
        if guess[i] == secretnumber[i]:
            clues.append('Fermi')
        elif guess[i] in secretnumber:
            clues.append('Pico')
    if len(clues) == 0:
        return 'Bagels'
    else:
        clues.sort()
        return ''.join(clues)
    if __name__ == '__main__':
        main()

def main():
    print('''
    Im thinking of a three digit number. Try to guess what it is.")
    Here are some clues:
    When I say:             That means:
    Pico                    One digit is correct and in the wrong place
    Fermi                   One digit is correct and in the right position
    Bagels                  No digit is correct.
    ''')

    maxguesses = 20

    secretnumber = secretnum()
    print('I have a number.')
    print('You have to guess it!'.format(maxguesses))

    numGuesses = 1
    while numGuesses <= maxguesses:
        guess = ''
        print('Guess #{}: '.format(numGuesses))
        guess = input('> ')

        clues = getclues(guess, secretnumber)
        print(clues)
        numGuesses += 1
        if guess == secretnumber:
            print("You got it!!!")
            ask()
        if numGuesses > maxguesses:
            print('You ran out of guesses.')
            print('The answer was {}.'.format(secretnumber))
            ask()

main()
1 Answers
def getclues(guess, secretnumb):
    if guess == secretnumber:

should be (replace the other occurences in the function too):

def getclues(guess, secretnumb):
    if guess == secretnumb:

secretnumber is the name of the variable in your main function scope, it doesn't exist out of it. Either rename the argument secretnumb to secretnumber, or replace all occurences of secretnumber inside of getclues with the name you gave it, secretnumb.

Related