No output after calling function (Python) [duplicate] - python

This question already has answers here:
What is the purpose of the return statement? How is it different from printing?
(15 answers)
Closed 6 months ago.
just a total newbie here. I am learning Python and was trying to call the function as below, yet I cannot see any output.
nums = [0,1,2,7,14]
def has_lucky_num(nums):
#return numbers that can be divisible by 7
for num in nums:
if num % 7 == 0:
return True
return False
has_lucky_num(nums)
Can someone tell me why it did not work? Thanks in advance.

Your program isn't printing anything because it doesn't have a print() command anywhere. Try the following line : print(has_lucky_nums(nums)), and it should print your True or False result! So you would have the following code :
nums = [0,1,2,7,14]
def has_lucky_num(nums):
#return numbers that can be divisible by 7
for num in nums:
if num % 7 == 0:
return True
return False
print(has_lucky_num(nums))

Your function has a problem:
You must put the return False after loop because if you don't it return immediately after checking first item
So function code must be this:
nums = [0,1,2,7,14]
def has_lucky_num(nums):
#return numbers that can be divisible by 7
for num in nums:
if num % 7 == 0:
return True
return False
If you want to see result you must print the result returns by the function has_lucky_num()
So all you have to do is to add a print like this:
Instead Of:
has_lucky_num(nums)
Replace With:
print(has_lucky_num(nums))
Output:
True

Return True and Return False won't actually display anything. You need to do something with those values, for instance:
if has_lucky_num(nums) == True:
print(f'{nums} has a lucky number!')
else:
print(f'{nums} has a lucky number!')
```

Related

Explain how python execute this function? [duplicate]

This question already has answers here:
What is a debugger and how can it help me diagnose problems?
(2 answers)
Closed 2 years ago.
def check_even_list(num_list):
# Go through each number
for number in num_list:
# Once we get a "hit" on an even number, we return True
if number % 2 == 0:
return True
else:
return False
now when execute function
[1]-check_even_list([1,2,3])
False
[2]-check_even_list([2,1,3])
True
why this True when #3 is return False ???
[3]-check_even_list([1,4,2])
False
Why this False when #2 is return True ???
Your code will only ever check the first element, once a return statement is hit, no further code in your function will execute. Instead you could modify your code to
def check_even_list(num_list):
# Go through each number
for number in num_list:
# Once we get a "hit" on an even number, we return True
if number % 2 == 0:
return True
# After loop, must not have hit an even number
return False
For conciseness, you could also write the equivalent
def check_even_list(num_list):
return all(i % 2 == 0 for i in num_list)
You are using "early return statements". What your function does is actually only evaluating the first element of your list.
Here's I would write the function:
def check_even_list(num_list):
return all(map(lambda x: x % 2 == 0, num_list))
This is because once return statement is executed, function is not executed any more.
Use this code:
def check_even_list(num_list):
# Go through each number
for number in num_list:
# Once we get a "hit" on an even number, we return True
if number % 2 == 0:
return True
# After loop is completed and no even values encountered
return False
and if you want to do just the opposite i.e. return false once an odd number is hit:
def check_odd_list(num_list):
# Go through each number
for number in num_list:
# Once we get a "hit" on an odd number, we return True
if number % 2 != 0:
return False
# After loop is completed and no odd values encountered
return True

Having difficulty getting my code to run correctly [duplicate]

This question already has answers here:
Why is "None" printed after my function's output?
(7 answers)
Closed 3 years ago.
Beginner programmer here working with Python and am currently experiencing a bug.
I am trying to write a function where, when a user inputs a number the machine will return True if the number input is 5. If the number input is any other number it will return as None.
Currently, when I enter 5 I will get True followed by None. If I enter any other number I will get None followed by None again. Anyone have any ideas?
Here is the code:
x = int(input('Enter a number: '))
def is_it_five (x):
if x == 5:
print(True)
else:
print(None)
print(is_it_five (x))
Your first value is coming from the loop, and the second value 'None' is coming from Print function, since you are not returning anything.
Either you can call the function without print statement:
is_it_five (x)
Or you can return it from the function and print.
x = int(input('Enter a number: '))
def is_it_five (x):
if x == 5:
return True
else:
return None
print(is_it_five (x))
x = int(input('Enter a number: '))
def is_it_five (x):
if x == 5:
return(True)
else:
return(False)
print(is_it_five (x))
Functions in Python always return some value. By default, that value is None.
So if I create any random function e.g
def get_string(some_string):
print(some_string)
and then execute it;
get_string("Example")
The output will be
Example
But, if you do print(get_string("Example"))
The output will change to
Example
None
This is because the
get_string("Example")
is treated as an object in this case when you pass it inside the print statement.
However, when you are calling the function without printing it, you are not printing the value which the function returns.
In the same context if you do something like
value = get_string(some_string)
Your string will get printed as usual, but now the value variable will contain None.
Your problem is that you are printing a function that already prints something when called. Replace
print(is_it_five(x))
with just
is_it_five(x)

Some questions about the following code

I have the following questions about the following code:
What value does 0 hold in the second line? Is it something like 'true' or 'false'? Or a numerical value?
Are the return statements necessary in the user_even function? The code works without them but it seems that all user-defined functions have a return statement in them or am I wrong?
def divisible(num1, num2):
return num1 % num2 == 0
def user_even():
num1 = int(input ("Choose a number: "))
num2 = int(2)
if divisible(num1, num2):
print ("It's even")
return
else:
print ("it's odd")
return
user_even()
For question 1, it evaluates the statement and returns a Boolean (True or False) value. The 0 is 0.
return 5 % 5 == 0 # Remainder of 5/5 is 0 so that returns True
return 5 % 4 == 0 # Remainder of 5/4 is 1 so that returns False
For question 2, the return statements are not needed. A return statement should be used for variables or pieces of data that need to be returned from the function. In the code you provided, there is no data being returned so there is no need for the return statement.
1.) Return will be boolean because you used Comparator Operators (==)
2.) return statement is not necessary. Refer to this thread.
Is it ok to skip "return None"?

Why does this iteration didn't return anything out? [duplicate]

This question already has an answer here:
Python recursion with list returns None [duplicate]
(1 answer)
Closed 7 years ago.
def is_power(a,b):
if a<b:
is_power(b,a)
elif a==b:
return True
else:
if a%b !=0:
return False
else:
is_power((a/b),b)
is_power(2,32)
I don't know why it didn't show anything out, but when I print the last line of the function "is_power((a/b),b)", it shows:
True
None
None
None
i write and run it in ipython notebook and python's version is 2.7.10.1
def is_power(a,b):
if a<b:
return is_power(b,a)
elif a==b:
return True
else:
if a%b !=0:
return False
else:
return is_power((a/b),b)
You're running a recursive function without returning anything on steps.
is_power(2, 32)
First step : if a < b: return is_power(32, 2)
Second step : (else condition): return is_power(16, 2)
Thrid step : (else condition): return is_power(8, 2)
Fourth step : (else condition): return is_power(4, 2)
Fifth step : (else condition): return is_power(2, 2)
Sixth step : (elif a==b): return True
Result: True
If you miss any return statement, the code won't return anything else than None
You have insert a return statement to the appropriate rows and have to add to the end of the code : print is_power(x,y),
which call the is_power() function and the return values to the output.
Note in IPython is_power(x,y) alone works too.
def is_power(a,b):
if a<b:
return is_power(b,a)
elif a==b:
return True
else:
if a%b !=0:
return False
else:
return is_power((a/b),b)
print is_power(2,32)
Output:
True
You're program is made to return a Boolean, hence you will either get a True or a False.
If you want a different output, you'll have to code it to generate something else.
The only 2 return statements in your code are these :
elif a==b:
return True
and :
else:
if a%b !=0:
return False
Therefore the only outputs you can expect are True and False.

Python - Getting prime numbers [duplicate]

This question already has answers here:
Fastest way to list all primes below N
(39 answers)
Closed 8 years ago.
I'm trying to get make a program that will print all the prime numbers within the argument I pass to it, so far I've made python respond with a True or False if it is prime or not.
prime = []
prime_list = []
def check_prime(user_input):
for x in range(2, user_input):
if float(user_input) % x > 0:
prime = True
else:
prime = False
break
print prime
check_prime(int(raw_input("get prime numbers up to: ")))
Here is the program where it successfully returns if the number is prime or not(I think)
What I am trying to do is get all the prime numbers and store them into a list, then print them all out when it's finished. I've been picking away at this program for at least a week and I just can't figure it out.
Please pointers only, no finished version.
You have made a function which prints out True or False depending on whether a number is prime.
The next step is to make it return True or false.
Here is an example of a different test which you can adapt for your own needs.
def is_even(number):
if number % 2 == 0:
return True
else:
return False
$ is_even(6)
True
$ answer = is_even(6)
$ print(answer)
True
The next step is to loop through all the numbers that you want to consider, and only store them if they are prime. (You could also just print them if that's all you need.)

Categories

Resources