This forum is now read-only. Please use our new forums! Go to forums
12/15 Semantic Error: Only one of each odd Number is Removed
I have encountered an error. The following code only removes one of each odd number from a list, and I need it to remove all of the odd numbers.
def purify(sequence):
new_list = sequence
for i in sequence:
if (i % 2 != 0):
new_list.remove(i)
return new_list
I understand that it would be much more effective to reverse the logic and create an empty list to .append()
even numbers to, and that is what I shall probably end up doing. I am curious, though, as to why this doesn’t work.
Any help would be greatly appreciated.
Answer 55adf44dd3292f6c20000653
when I run your code in Idle, it removes all odd numbers, so I am not sure why it says it only removes one of your odd number for a list. It works perfectly fine.
Answer 55ae5bcd9113cb9dc60002e3
def purify(num): b=[] for a in num: if a%2==0:b.append(a) return b
#it works
Answer 55af1cf776b8feec66000600
def purify(numbers): -new_list = [] -for num in numbers: –if num % 2 == 0: —new_list.append(num) –return new_list
n = raw_input(“enter the numbers”) print purify(n)
enter the numbers 1,2,3
Error message : Traceback (most recent call last): File “python”, line 10, in
can anyone explain what am i doing wrong?
2 comments
I think the problem is that n
is stored as a string instead of an array. If you were to call purify([1,2,3])
, then it should work.
it did work when i sent [1,2,3] as argument but its not working when i converted the input (string format ) into integer
Answer 55af2a639376768d630000d9
def purify(numbers): -new_list = [] -for num in numbers: –a = int(num) –if a % 2 == 0: —new_list.append(num) -return new_list
n = []
n = raw_input(“enter the numbers”)
print purify(n)
Error :- Traceback (most recent call last): File “python”, line 11, in
1 comments
n
is still being stored as a string. Although it was initialized as a list, it was reassigned as the result of raw_input("enter the numbers")
. If you want to turn the user’s input into a list, consider using the .split()
method, which separates a string into a list whenever it sees what is in the parentheses. n.split(“,”) would return the list [“1”, “2”, “3”] if you typed “1,2,3” without quotation marks when given the prompt “enter the numbers”. You could then convert the individual elements of the lists to numbers by using the int()
method.
Popular free courses
- Free Course
Learn SQL
In this SQL course, you'll learn how to manage large datasets and analyze real data using the standard data management language.Beginner friendly,4 LessonsLanguage Fluency - Free Course
Learn JavaScript
Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.Beginner friendly,11 LessonsLanguage Fluency - Free Course
Learn HTML
Start at the beginning by learning HTML basics — an important foundation for building and editing web pages.Beginner friendly,6 LessonsLanguage Fluency
3 comments
maybe the function is called with a 0 in
sequence
and codeacademy defines 0 as odd.The problem that I kept running into was when they called the function with [4,5,5,4]. My code kept returning [4,5,4].
@Travis Hey I think I might have found an issue. Again, I’m just learning as well so i might be wrong. you used new_list.remove(i) in for loops, and from what I learned, for loops stores the length of your initial list. For example, [4,5,5,4] has length 4. beginning of your for loops, python thinks your list has 4 length, and starts to run your code starting from index [0]. When it met your code ‘new_list.remove(i)’, it does what it is told to do. remove 5, and this is where the issue occurs. python thinks that your list has length of 4. however, by removing one of your item, your code now has length 3. -> your remove code runs at index [1], which from original list, it’s 5. -> then your for loop code runs again to next index which is [2]. -> but because you removed first 5 from your original list, index[3] is now 4. This is why I think you gain [4,5,4] as your output.
To be clear, index [0] - 4 index[1] -5 —-> removes this from the list, now list has [4,5,4] index[2] - 4 no more items, exits the for loop code. output = [4,5,4]