Am I doing something wrong? There was no errors running the program. This should output a boolean value, but when I run it there is no return value. The program lets the user to enter a number, but then the program doesn't return anything.
def main():
num = int(input("Enter a number:"))
isPrime(num)
def isPrime(num):
if num < 2:
return False
elif num == 2:
return True
else:
for counter in range(2, num):
if num % counter == 0:
return True
return False
main()
Do you mean to print isPrime (num)?
No, the return statement itself will not print to the console. The print statement does that.
"return" only returns that value from the function, it does not display anything to the screen. You could assign the value that is being returned to a variable, but if it is not assigned or used, it is lost. If you wanted it to actually be displayed, you could do
print(isPrime(num))
which will then print out the resulting boolean to the console, or assign the returned value to a variable for later use.
You may do like this
result = isPrime(num)
print(result)
Related
This question already has answers here:
Why does my recursive function return None?
(4 answers)
Closed 25 days ago.
def myfun1(count):
count += 1
if count == 10:
return count
print(count)
myfun1(count)
if __name__ == '__main__':
print(myfun1(0))
I want to return the count and terminate the recursion when count == 10.
The above code returns None when count == 10. Could someone please elaborate?
Instruction myfun1(count) performs a recursive call but you don't do anything of the value returned by this call. You need to return a value when you reach the end of your function. Otherwise, when the end of the function is reached, you have an implicit return None statement.
So if you want to return the result of your recursive call, do something like that:
def myfun1(count):
count += 1
if count == 10:
return count
print(count)
result = myfun1(count)
return result
if __name__ == '__main__':
print(myfun1(0))
Generally speaking when a function (recursive or not) is supposed to return something that it computed, make sure that it ends with a return statement.
First you need Else to return a different value in return, instead you will always return 10 in your recursion
def myFun1(count):
count += 1
if count == 10:
return count
else:
return count
and you have refactore some points of your function to make the recursion, call it again
def myFun1(count):
count += 1
if count == 10:
return count
else:
count_res = myFun1(count)
return count_res
and you have just call the function
if name == 'main':
print(myFun1(0))
n = int(input())
def reverse(num):
while num > 0:
return f"{num}{reverse(num - 1)}"
result = reverse(n)
print(result)
I have some code lines as above. (Incase Input n = 5)It prints "54321None" instead of "54321" as I expected.
Please help me
The last iteration will always be a 0. If you just call reverse(0) it will return None, so that is the problem.
Without rewriting too much code the 'simple' solution could be:
return f"{num}{reverse(num-1)if num-1 != 0 else ''}"
This only executes the reverse function if the variable you pass into reverse is not 0. Otherwise it will return Nothing.
def inputList():
list=[]
length=int(input("Enter the length of the list : "))
print("Enter the elements -->")
for i in range(length):
print(i)
try:
element=int(input(":"))
list.append(element)
except:
print("Invalid element entered! WON'T BE COUNTED!")
i-=1
print("i NOW :",i)
print("Unaltered list :",list)
return list
I am trying to code the except block such that it also decreases the value of i if an error occurs (Eg. Entering a float instead of int). When the except block runs it prints the decreased value but when the for runs again i remains unchanged and therefore the total length is one less than what it should be (because of one incorrect value).
you can view in a different way and use this, this is more simple and avoid the substract in the counter
def inputList():
list =[]
print("Unaltered list :",list)
length =int(input("Enter the length of the list: "))
print("Enter the elements -->")
i=0
while(i<length):
print(i)
try:
element = int(input(":"))
list.append(element)
i++
except:
print("Invalid element entered! WON'T BE COUNTED!")
print("i NOW :",i)
return list
Does it work if you put your call of i in the last except print statement within str()?
print("i NOW : "+ str(i))
SPY GAME: Write a function spy_game() that takes in a list of integers and returns as per cases-
Case 1: spy_game([1,2,4,0,0,7,5]) --> '007'
Case2: spy_game([1,7,2,0,4,5,0]) --> '700'
This below code is not working. My spy_list is also changing even it is outside the for loop
def spy_game(nums):
spy_num=''
spy = True
spy_list=nums
for num in nums:
while spy:
if num == 0 or num == 7:
spy_num += str(num)
spy_list.pop(0)
break
else:
spy = False
while not spy:
spy_list.pop(0)
spy = True
return (spy_num)
While when I pass the list itself instead of nums in for loop, it works
please suggest
def spy_game(nums):
spy_num=''
spy = True
spy_list=nums
for num in [1,0,2,4,0,5,7]:
while spy:
if num == 0 or num == 7:
spy_num += str(num)
spy_list.pop(0)
break
else:
spy = False
while not spy:
spy_list.pop(0)
spy = True
return (spy_num)
I do not know python very well but I do see the issue. When setting
syp_nums = nums
you are stating that these two lists are they same so one action applys to the other one. You need to initialize a new list for spy nums that have the same value. Python will also allow you to copy
see this stackoverflow question How to clone or copy a list
spy_nums = nums.copy()
why does the following code returns none:
j = 22
def test(j):
if j > 0:
print('j>0')
else:
print('j<0')
Output:
j>0
None
A function in Python always has a return value, even if you do not use a return statement, defaulting to None
Because the test function doesn't return a value, it ends up returning the object None. that's why it ended up printing None Since you do not have a return value specified
you may not use print in your function, but return a string instead
def test(j):
if j > 0:
return 'j>0'
else:
return 'j<0'
then call it like this: print it when calling the function
print(test(22))
see answer's here for more detail