The even_numbers function returns a space-separated string of all positive numbers that are divisible by 2, up to and including the maximum that's passed into the function. For example, even_numbers(6) returns “2 4 6”. Fill in the blank to make this work.
def even_numbers(maximum):
return_string = ""
for x in range (0,maximum+1,2):
return_string += str(x) + " "
return return_string.strip()
print(even_numbers(6)) # Should be 2 4 6
print(even_numbers(10)) # Should be 2 4 6 8 10
print(even_numbers(1)) # No numbers displayed
print(even_numbers(3)) # Should be 2
print(even_numbers(0)) # No numbers displayed
Answers
Answer:
def even_numbers(maximum):
return_string = " "
for x in range(2,maximum+1):
if x%2==0:
return_string += str(x) + " "
return return_string.strip()
print(even_numbers(6)) # Should be 2 4 6
print(even_numbers(10)) # Should be 2 4 6 8 10
print(even_numbers(1)) # No numbers displayed
print(even_numbers(3)) # Should be 2
print(even_numbers(0)) # No numbers displayed
Explanation:
Answer:
The following Python Program performs the desired function:
def even_numbers(maximum):
return_string = " "
for x in range(2,maximum+1):
if x%2==0:
return_string += str(x) + " "
return return_string.strip()
print(even_numbers(6)) # Should be 2 4 6
print(even_numbers(10)) # Should be 2 4 6 8 10
print(even_numbers(1)) # No numbers displayed
print(even_numbers(3)) # Should be 2
print(even_numbers(0)) # No numbers displayed
Explanation:
Even numbers are those numbers that are completly divisible by two. For exmple, 2,4,6,90, 88 etc
To solve the above problem, you need to implement some condition that can identify whether the number is divisible by 2 or not.
So for this, we will write:
for x in range(2,maximum+1):
if x%2==0:
return_string += str(x) + " "
modulo operation will check the remainder and if it is zero it means that it is an even number.
After that return statement, just append the even number to the string.
- print(even_numbers(6))
It will display 2 4 6 because 2,4, 6 are even numbers in the range of 6.
- print(even_numbers(10))
Similarly, It will display 2 4 6 8 10
- print(even_numbers(1))
It will display No numbers since there is no even number in the range of 1
- print(even_numbers(3))
It will display 2
- print(even_numbers(0))
It will display No numbers.
#SPJ2