def display_even_digits(a, b):
for digit in range(a, b):
if digit % 2 == 0:
return digit
print(display_even_digits(1, 101))
Asked
Active
Viewed 66 times
-1

Random Davis
- 6,662
- 4
- 14
- 24

Keldro
- 33
- 6
-
5The function returns as soon as the first even number is found. – sj95126 Sep 23 '22 at 19:19
-
2`return` returns the value, terminating the function. If you want a list, then you need to build a list and `return` it at the end of the function. Later on, you'll learn about `yield`, which is closer to what you're trying to do here, but don't start trying to write generator functions until you understand regular functions first. – Silvio Mayolo Sep 23 '22 at 19:21
-
Yes, but I used a "for" loop, so I thought it would iterate through the entire range. And when I use "print", it returns the expected result, but with the word NONE at the end. – Keldro Sep 23 '22 at 20:16
2 Answers
0
You are returning the first even number, you need to return all numbers. You could just find if a is even and if not add 1 and then use the step argument of range.
def display_even_digits(a, b):
return list(range(a + a % 2, b, 2))
print(display_even_digits(1, 101))
[2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100]

Jab
- 26,853
- 21
- 75
- 114
-
I understand now, but how to do it if I want to display the result of the function not as a list. Just like this: 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100 When I use print instead of return it results in the interpreter adding the word "None" to my end – Keldro Sep 23 '22 at 20:03
-
@Keldro that already been covered in previous questions on this site. As for why `None` is being printed, it's because if you change a function from one that returns something, to one that only prints something, but you still are trying to print the returned value of the function, `None` will be printed because the function did not return anything. And `None` is the implicitly returned value for any function that does not specify an explicit returned value. – Random Davis Sep 23 '22 at 20:13
-
Thank you @Random Davis. To learn a programming language is one thing, but to understand the principle of operation is another, a little more difficult. – Keldro Sep 23 '22 at 20:23
-
I mentioned it has been asked before because on this site, you have to not only keep it to one question per post, but also, you can't post duplicates of old questions. [here](https://stackoverflow.com/questions/11178061/print-list-without-brackets-in-a-single-row) is an old question that ought to help. – Random Davis Sep 23 '22 at 20:33
0
This is probably what you want?
def display_even_digits(a, b):
result = []
for digit in range(a, b):
if digit % 2 == 0:
result.append(digit)
return result
print(display_even_digits(1, 101))
[2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100]

Yu-Fen
- 43
- 9