I have a homework to do in python class and was given this question:
Make a program that gets 2 numbers from the user, and prints all even numbers in the range of those 2 numbers, you can only use as many for statements as you want, but can't use another loops or if statement.
I understand that I need to use this code:
for num in range (x,y+1,2):
print (num)
but without any if statements, I can't check if the value x inserted is even or odd, and if the user inserted the number 5 as x, all the prints will be odd numbers.
I also tried to enter each number to a tuple or an array, but I still can't check if the first number is even to start printing.
def printEvenFor(x,y):
evenNumbers =[]
for i in range (x,y+1):
evenNumbers.append(i)
print (evenNumbers[::2])
or
def printEvenFor(x,y):
for i in range (x,y+1,2):
print(i,",")
I expect the output of printEvenFor(5,12) to be 6,8,10,12 but it is 5,7,9,11
Aucun commentaire:
Enregistrer un commentaire