i'm a begginer at phyton, and i am trying to create a function that indicates if is true or false if an even number is true or false.
This function is already working, but now i am trying to pass the function with a value given through the input function.
This is my code:
def isprime (value):
value=int(input("Enter a number: "))
if value < 2:
return False
else:
for count in range (2, value):
if value % count == 0:
return False
return True
isprime ()
When i run isprime () i get the following error message
TypeError: isprime() missing 1 required positional argument: 'value'
Please, note that if i don't put the input part and i run the funcion like isprime (3) my output is TRUE.
Can you please help me to let me know what i am doing wrong?
CodePudding user response:
You just need to call the function with an argument int(input("Enter a number: ")), so your code might be :
def isprime(value):
if value < 2:
return False
else:
for count in range (2, value):
if value % count == 0:
return False
return True
isprime(int(input("Enter a number: ")))
CodePudding user response:
The issue here is you need to pass the value to make the code work. However, you are taking the input inside the function. Therefore, you need to take the input outside the function.
value = int(input("Enter a number: "))
isprime(value)
CodePudding user response:
Your function require one parameter but you don't give it.
Try this:
def isprime (value):
if value < 2:
return False
else:
for count in range (2, value):
if value % count == 0:
return False
return True
value = int(input("Enter a number: "))
isprime (value)
CodePudding user response:
It seems like you're defining a required input to the function, but over-writing it with input from stdin. So do you want to use the input to the function or discard it ? In case you want to keep the input to the function optional, try
def isprime (value = None):
if value is None:
value=int(input("Enter a number: "))
if value < 2:
return False
else:
for count in range (2, value):
if value % count == 0:
return False
return True
"None" is similar to a "null" value in python. NoneType in Python is a data type that simply shows that an object has no value/has a value of None. By setting value = None
you're allowing for an optional input.
In this case if you execute isprime()
it will ask the user for an input because value = None. If you execute isprime(some_integer)
it will use value = some_integer.
Btw, if you want to check if a number is prime or not there is a more elegant way to do using recursion.