Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

My function is somehow printing one value being held by a variable but returning something different!

So, as you will see in my code, I am trying to test a piece of code (not very well written, sadly) that will force the user to input integers.

def create_game():
    #Get user input difficulty levels.
    print('create game')

    amount = input('How many items do you want to remember? ')
    try:
        int(amount)        
        print(amount)
        print('try')
    except ValueError:
        print(amount)
        print('except')
        print('Value must be a number greater than 0...')
        create_game()

    time_guess = input('Set your time limit (in minutes): ')
    try:
        int(time_guess)
        print(time_guess)
        print('try')
    except ValueError:
        print(time_guess)
        print('except')
        print('Value must be a number greater than 0...')
        create_game()

    print('amount: {} / time: {}'.format(amount, time_guess))
    return int(amount), int(time_guess)

amount, time_guess = create_game()
print('amount: {} / time: {}'.format(amount, time_guess))

(most of the print statements are for debugging purposes) So, if I do some basic testing, say, input the letter 'a' for the amount, it does what it should and spits me back to the top. So, I put in 4 for the amount and then it moves me to the next input. I type 't' for the time_guess and then in spits me back to the top (as it should). I then type 4 for the amount and 3 for the time_guess. Both pass the test and it prints out "amount: 4 / time: 3" and then returns amount and time_guess.

Now, when it prints after the function call to create_game() (the last line in the code) it prints "amount:4 / time: t" along with this error message (generated in IDLE):

Traceback (most recent call last):
  File "C:/Python34/Scripts/Linker Memory game/create game test.py", line 6, in create_game
    int(amount)
ValueError: invalid literal for int() with base 10: 'y'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:/Python34/Scripts/Linker Memory game/create game test.py", line 29, in <module>
    amount, time_guess = create_game()
  File "C:/Python34/Scripts/Linker Memory game/create game test.py", line 13, in create_game
    create_game()
  File "C:/Python34/Scripts/Linker Memory game/create game test.py", line 27, in create_game
    return int(amount), int(time_guess)
ValueError: invalid literal for int() with base 10: 't'

so it appears to be printing the most current amount and time_guess (4 and 3) when in the function but returning the previously entered amount and time_guess (4 and 't'). I am stymied.

Any help would be appreciated.

1 Answer

Hi,

It should ok if you call return create_game() instead of create_game() in your two except blocks. I think it's returning None to your amount and time_guess. Let me know if this works.

def create_game():
    #Get user input difficulty levels.
    print('create game')

    amount = input('How many items do you want to remember? ')
    try:
        int(amount)        
        print(amount)
        print('try')
    except ValueError:
        print(amount)
        print('except')
        print('Value must be a number greater than 0...')
        return create_game()

    time_guess = input('Set your time limit (in minutes): ')
    try:
        int(time_guess)
        print(time_guess)
        print('try')
    except ValueError:
        print(time_guess)
        print('except')
        print('Value must be a number greater than 0...')
        return create_game()

    print('amount: {} / time: {}'.format(amount, time_guess))
    return int(amount), int(time_guess)

amount, time_guess = create_game()
print('amount: {} / time: {}'.format(amount, time_guess))