- 1
What is wrong with my code
numbers =list(range(1000)) odd_numbers=[ ] even_numbers=[ ] for x in numbers: if ( x%3==0): odd_numbers.append(x) else: even_numbers.append(x) print (list(odd_numbers)) print (list(even_numbers))
11 Antworten
+ 1
Yusuf Jamal
numbers =list(range(1000))
odd_numbers=[ ]
even_numbers=[ ]
for x in numbers:
if ( x%2!=0):
odd_numbers.append(x)
else:
even_numbers.append(x)
print(odd_numbers)
print(even_numbers)
+ 3
Yusuf Jamal
Mistakes in your code,
1)x%3==0
This statement means if x is only divisible by 3.Not means x is odd. x%2!=0 means x is not divisible by 2 . So it takes odd numbers. But if you use x%2==0 it takes only a even numbers.
2)list(odd_numbers)
You have already created a list .
So, there is no use to use list() function.
+ 2
Well you can also do like this - https://code.sololearn.com/c1A2I4JA2Q82/?ref=app
+ 1
Hi Yusuf Jamal ,
Please show us your code! Then only we can help you!
Stick your code!
+ 1
Print statement is inside the loop, that's why it is printing multiple list
0
Yusuf Jamal
where is your code?
0
numbers =list(range(1000))
odd_numbers=[ ]
even_numbers=[ ]
for x in numbers:
if ( x%3==0):
odd_numbers.append(x)
else:
even_numbers.append(x)
print (list(odd_numbers))
print (list(even_numbers
0
Correct!
Why is it returning multiple list before?
0
Oh! Thanks
0
Thank you!
0
https://code.sololearn.com/cA0A902A0A6A
numbers =list(range(1000))
odd_numbers=[ ]
even_numbers=[ ]
for x in numbers:
if ( x%2!=0):
odd_numbers.append(x)
else:
even_numbers.append(x)
print (list(odd_numbers))
print (list(even_numbers))