New course! Every coder should learn Generative AI!
Try a free lesson+ 1
print(" ".join([i for i in input().split() if int(i)%2==0]))
https://code.sololearn.com/c3LX0QtX8r4Z/?ref=app
+ 9
Nikolay ,
the reason for your issue is that elements in the nums list will be removed during iteration. this will result in index issues.
this can be avoided when we use a copy of our list to iterate through:
...
for i in nums[:]:
...
following the task description that says:
▪︎"Given a list of numbers, you want to TAKE OUT all of the odd ones and LEAVE just the even ones."
the above code will do this.
▪︎a different approach to get the even values out of the list is to use a list comprehension, but it does not strictly follow the description as it creates a new list:
# the code in line 1 starting with < or ...> is just to make input easy for this test. it needs only to press < Return > to pass the numbers to the input() function
lst = list(map(int, input().split() or "8 10 19 25 5 16 12".split()))
print([num for num in lst if num % 2 == 0])
+ 3
To find even numbers you have to use 2 mod equal to 0 then print the even number
nums = input().split(" ")
for i in nums:
if int(i) % 2 == 0:
print(i)
I hope it is clear to you
+ 2
Shadoff plz don't give so difficult code to the beginners which would made them confuse. Try to solve their doubts don't give them different solutions
+ 1
Aysha , and your code is correct and no any confuses?
How many line your code will print out? Huh
0
#Ruby
s = gets.chomp.split(" ")
s.each do |x|
print "#{x} " if x.to_i % 2 == 0
end
0
Shadoff teach me one liner solution 🥹🥺