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
Related
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!')
```
i mathematically find it difficult to compose a code to fill the line 4 in the below code
is_prime = True
for i in range(2,n):
if n%i == 0:
print("impossible")
print(is_prime)
i have been typing print(string) i.e
print("this is not possible")
in the blank and it output
true
what is the correct code to write. i want to believe it is a mathematics expression thing.i don't know the code to write after if.
If n is a prime number, then n%i == 0 will always be false in your loop. That's why the print in the 4th line is never called (you said it yourself, it's impossible). The true you are getting is from the print on the last line: you never update the value of is_prime, so it will always print True.
On the other hand, if what you want to know is if n is a prime number, you can do this:
is_prime = True
for i in range(2,n):
if n%i == 0:
is_prime = False
break
print(is_prime)
here i wrote it down for you, getting to mathematics what we know is any number is prime number if it can give a integer when it is divided by one or itself so now we can check if the number is divisible by any number in between
for example if your number is equal to 5 then we check if 5 gives an integer and leaves no remainder when divided by 2,3 and 4, you can program this algorithm as follows
x=8 #any number to check for
answer= "prime"
for i in range(1,x-1):
if(x%(i+1) == 0 ):
answer="non-prime"
print(answer)
The problem is that you are not reassigning is_prime to False when your if condition is passed. After print("impossible) you can simply add is_prime = False and follow that with a break statement to exit the for loop.
print(is_prime) will now return False.
You can also modify range(2, n) to range(2, n//2) (the // operator performs integer divison and leaves no remainder). This is because for any given number, you will only need to check half of numbers from 1 to n, as eventually you will start to encounter repeated numbers.
is_prime = True
for i in range(2, n //2):
if n % i == 0:
print("impossible")
is_prime = False
break
print(is_prime)
Edit print("impossible") to any of the following two approaches:
is_prime = False
or
not any([n%i==0 for i in range(2,n)])
from your code, the print statement is on the outer scope from the if condition. You woud try adding an else block. I have have included an example using your code. Hope it helps.
is_prime = True
n =10
for i in range(2,n):
print(i)
if n%i == 0:
print("impossible")
else:
print(is_prime)
My goal is to make a program that prints onscreen all of the prime numbers it can find, however I have this issue where the while loop runs only once, instead of repeating forever.
def isPrime(num):
if num < 2:
return False
if num == 2:
return True
if num % 2 == 0:
return False
i = 3
while i * i <= num:
if num % i == 0:
return False
i += 2
x = 1
while True:
x += 1
if isPrime(x):
print (x)
I also have tried adding print("You can see this.") at the very end of the code, and it runs, but only once.
I'm sure it's a common mistake, since Python is very strict on indentation, but could you guys help me discover it? Thanks in advance.
You need to return True at the very end, after and outside the final loop. Otherwise the function ends without explicitly returning anything, so it implicitly returns None which is interpreted as false.
The i +=2 is indented too deep, the inner loop never quits.
It should read
while i * i <= num:
if num % i == 0:
return False
i += 2
Otherwise i never increases and the loop goes on and on and on.
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.
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.)