- 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))

15th Jun 2021, 6:42 AM
Yusuf Jamal
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)
15th Jun 2021, 6:50 AM
˜”*°•.˜”*°• Mohan 333 •°*”˜.•°*”˜
˜”*°•.˜”*°• Mohan 333 •°*”˜.•°*”˜ - avatar
+ 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.
15th Jun 2021, 7:36 AM
˜”*°•.˜”*°• Mohan 333 •°*”˜.•°*”˜
˜”*°•.˜”*°• Mohan 333 •°*”˜.•°*”˜ - avatar
+ 2
Well you can also do like this - https://code.sololearn.com/c1A2I4JA2Q82/?ref=app
15th Jun 2021, 7:15 AM
Abhiyantā
Abhiyantā - avatar
+ 1
Hi Yusuf Jamal , Please show us your code! Then only we can help you! Stick your code!
15th Jun 2021, 6:45 AM
Abhiyantā
Abhiyantā - avatar
+ 1
Print statement is inside the loop, that's why it is printing multiple list
15th Jun 2021, 7:10 AM
Nanda Balakrishnan
0
Yusuf Jamal where is your code?
15th Jun 2021, 6:43 AM
˜”*°•.˜”*°• Mohan 333 •°*”˜.•°*”˜
˜”*°•.˜”*°• Mohan 333 •°*”˜.•°*”˜ - avatar
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
15th Jun 2021, 6:48 AM
Yusuf Jamal
0
Correct! Why is it returning multiple list before?
15th Jun 2021, 6:54 AM
Yusuf Jamal
0
Oh! Thanks
15th Jun 2021, 7:11 AM
Yusuf Jamal
0
Thank you!
15th Jun 2021, 7:18 AM
Yusuf Jamal
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))
17th Jun 2021, 1:53 AM
Abhishek Kumar
Abhishek Kumar - avatar