Python Forum
missing 1 required positional argument
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
missing 1 required positional argument
#1
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'
Reply
#2
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.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
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.
Reply
#4
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):
Reply
#5
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  TypeError: __init__() missing 1 required positional argument: 'successor siki 1 4,290 Mar-08-2021, 02:05 PM
Last Post: Larz60+
  Missing 1 required positional argument in python code edwinostby 7 9,868 Jan-19-2021, 12:52 PM
Last Post: Serafim
  Missing positional arguments error?? hhydration 2 2,128 Oct-01-2020, 05:33 AM
Last Post: buran
  TypeError: Missing required positional arguments liaisa 7 28,858 Sep-25-2020, 08:16 PM
Last Post: deanhystad
  missing positional argument error programmert 1 2,804 Oct-18-2019, 11:05 AM
Last Post: Larz60+
  missing 1 required positional argument mcgrim 10 19,721 May-07-2019, 09:02 PM
Last Post: Yoriz
  TypeError: __init__() missing 3 required positional arguments Pythonhelp82 6 23,067 Jan-24-2019, 04:25 AM
Last Post: Pythonhelp82
  TypeError: method missing 1 positional argument koolinka 4 5,012 Nov-18-2018, 04:53 PM
Last Post: ichabod801
  another positional argument error (...and executing objects stored in a list) itmustbebunnies 7 4,195 Nov-16-2018, 07:18 PM
Last Post: itmustbebunnies
  Class positional argument error itmustbebunnies 2 2,985 Nov-07-2018, 11:09 AM
Last Post: stullis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020