Python Forum

Full Version: missing 1 required positional argument
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm trying to debug this and I'm not seeing where "limit" is not being called into "play_game"

#!/usr/bin/env python3

import random

def display_title():
    print("Guess the number!")
    print()

def get_limit():
    limit = int(input("Enter the upper limit for the range of numbers: "))
    return limit

def play_game(limit):
    number = random.randint(l, limit)
    print("I'm thinking of a number from 1 to " + str(limit) + "\n")
    while True:
        guess = int(input("Your guess: "))
        if guess < number:
            print("Too low.")
            count += 1
        elif guess >= number:
            print("Too high.")
            count += 1
        elif guess == number:
            print("You guessed it in " + str(count) + " tries.\n")
            return

def main():
    display_title()
    again = "y"
    while again.lower() == "y":
        limit = get_limit()
        play_game()
        again = input("Play again? (y/n): ")
        print()
    print("Bye!")

# if started as the main module, call the main function
if __name__ == "__main__":
    main()
the error code I'm getting is
File "c:\Users\Jason\Downloads\p5-2_guesses_1.py", line 40, in <module>
main()
File "c:\Users\Jason\Downloads\p5-2_guesses_1.py", line 33, in main
play_game()
builtins.TypeError: play_game() missing 1 required positional argument: 'limit'
I really don't understand how it is possible not to see "where" - the traceback give you the row number. It's line 33 in this case...

And please, use BBCode tag error when post traceback.
You define the function like so:
def play_game(limit):
...but you try using the function like so:
play_game()
The interpreter is letting you know that you're trying to call a function which requires an argument, but you didn't give it any arguments.
Buran,

Poor word choice on my part - I didn't see how the function named "limit" wasn't being called down as an argument in line 13.

I'm new to coding in general and newer still to this forum - I'll review through the procedures for proper posting.

Nilamo,

I thought the argument was in there - it was the return value from get_limit

def get_limit():
    limit = int(input("Enter the upper limit for the range of numbers: "))
    return limit
and defined in play_game

def play_game(limit):
Look at your code above - in line 33 you call play_game() without an argument, but the definition requires one argument.

One way around this, besides the obvious of changing line 33 to
play_game(limit)
would be to change the definition to have a default. Change line 13 to
def play_game(limit=10):
This will give a default of 10. That will get rid of the error but will ignore the value entered by the user since you still aren't passing it to play_game().
Another way to get rid of the error and get the behavior you want would be to combine lines 32 and 33 :
        play_game(get_limit())
But the bottom line is that you have currently defined play_game as requiring one parameter to be passed, and you aren't giving it any.

Strong suggestion - don't use the same variable names inside a function as you do outside. Scope of variables will bite you. You have limit in get_limit(), in play_game() and in main(). It is easy to forget that these are 3 different variables.