Форум сайта python.su
этот вариант выдает четные и нечетные числа вперемешку, randomly
Homework 2
Enter the number of elements in the list: 5
Add a number to the list: 56
Add a number to the list: 54
Add a number to the list: 55
Add a number to the list: 32
Add a number to the list: 33
Your list includes: [56, 54, 55, 32, 33]
Your new list is [56, 55, 32]
>>>
Отредактировано (Окт. 3, 2010 13:54:08)
Офлайн
Мдя, что-то наглючил я :) Вам нечетные числа нужно удалять, а не элементы. Тогда так:
for i in new_li:
if i%2:
new_li.remove(i)
Офлайн
вот ответ с новым кодом,
Homework 2
Enter the number of elements in the list: 7
Add a number to the list: 76
Add a number to the list: 77
Add a number to the list: 79
Add a number to the list: 45
Add a number to the list: 41
Add a number to the list: 43
Add a number to the list: 83
Your list includes: [76, 77, 79, 45, 41, 43, 83]
Your new list is [76, 79, 41, 83]
>>>
print "Homework 2"
num_el=input("Enter the number of elements in the list: ")
new_li=[]
for x in range(num_el):
your_num=input("Add a number to the list: ")
new_li.append(your_num)
print "Your list includes: ", new_li
for i in new_li:
if i%2:
new_li.remove(i)
print "Your new list is",new_li
Отредактировано (Окт. 3, 2010 15:11:59)
Офлайн
Блин. Мне это уже начинает надоедать. 3 строчки кода, но правильно еще ни разу не написал…
for i in new_li[:]:
if i%2:
new_li.remove(i)
Офлайн
РАБОТАЕТ!!! Спасибо!
P.S. простите за хлопоты…
Офлайн