Reputation: 155
this is my first post on this website, i'm taking an online course for intro to programming and it's pretty tough for an intro. I have no previous experience with coding and python is the first language i'm learning so please forgive me if I'm doing something wrong on the forum...Anyways here is the question:
So my question is, how can i satisfy the arguments in a function ?. For example, i have a function that checks some things from previous code (i'm writing a shakespearian insult generator with some extra stuff like alphabetically sorting, saving, file I/O, etc.), and I cannot get my def
function to work...
It keeps giving me a missing 1 potential argument numInsults
error.
How can i satisfy this condition?
def checkInsultsFile(numInsults, Insults="Insults.txt"):
numInsults = number
isItAlpha = "Insults.txt".isalpha()
if isItAlpha:
return True
I need this function to return true if my insults text file is alphabetical and if number of insults(numInsults)
is legal (within 100-10000 and an integer) which i made sure it was legal using another function called getNumInsults
which = numInsults
and number is from a previous function...
number = int(input("\nHow many insults do you want? \
Enter in range 100 to 10,000: "))
I hope I included everything for an answer...My summerized question is: How can i stop getting the error?
Upvotes: 0
Views: 609
Reputation: 2332
How are you calling the function? You've defined it to take 1-2 arguments, but it needs at least 1 argument My guess is, elsewhere in your code, you have:
isOk = checkInsultsFile()
when what you should have is:
isOk = checkInsultsFile(101)
or, with the optional argument:
isOk = checkInsultsFile(101, "myInsults.txt")
EDIT:
Although OP didn't say, I believe the function in question is actually part of a class, in which case its first argument should be self
. So the definition should actually be:
def checkInsultsFile(self, numInsults, Insults="Insults.txt"):
...
although you would still call the function as above.
Upvotes: 0
Reputation: 3585
I see a couple of problems with this code, may be you could fix those and try
In the following two lines
def checkInsultsFile(numInsults, Insults="Insults.txt"):
numInsults = number ## Wrong: number is not known and numInsults assigned not a good idea)
There is a problem - you are passing numInsults
to the function and assigning value to it again in the next line. That's certainly not desired/expected. numInsults
will be available in your program when you pass it as a parameter. Also number
is not defined anywhere, is it defined somewhere else in the file? That's a potential error. Remember - usually you should not assign values to the parameters passed to the function (There are cases in other programming languages where it might be required/useful but not here).
The second argument has a default value "Insults.txt", this is useful in Python and it let's you call the function checkInsults in following way - all three are legal
checkInsults(10) # no value for Insults parameter - default is used
checkInsults(10, "another_insult.txt") # Insult is now set to "another_insult.txt"
However following is illegal
checkInsults() # Here numInsults an integer is expected to be passed and you are passing nothing
so this should give an error. `Expects 1 argument got 0 or something like that, but your error condition is something not I have commonly seen in Python.
Hope that helps to get you started and fix the code and hopefully fix the error yourself.
Upvotes: 1