Найти - Пользователи
Полная версия: нужен совет
Начало » Python для новичков » нужен совет
1 2
Nata
этот вариант выдает четные и нечетные числа вперемешку, 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]
>>>
Vader
Мдя, что-то наглючил я :) Вам нечетные числа нужно удалять, а не элементы. Тогда так:
for i in new_li:
if i%2:
new_li.remove(i)
Nata
вот ответ с новым кодом,
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
может, ошибка в другом месте?
Vader
Блин. Мне это уже начинает надоедать. 3 строчки кода, но правильно еще ни разу не написал…
for i in new_li[:]:
if i%2:
new_li.remove(i)
Nata
РАБОТАЕТ!!! Спасибо!
P.S. простите за хлопоты…
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Powered by DjangoBB