Purify


#1

Hello dear friends,
I have solved the question about purify but I have a question if you dont mind.
I would like to take a list of number from a user, and to test the question.
But I am searching for how I can take a list of numbers from a user?
As I know, by input(), I will have string and not int.
I can use int(input()) which do not work.
As you see in my code below, I have provided a list myself and my code works.
How if I need to take a list from a user?
Thank you in advance for any help from you <3

def purify(listi):
    new_list = []
    for i, n in enumerate(listi):
        if n % 2==0:
            new_list.append(n)
    return(new_list)
    
my_list = [2, 3, 5, 6, 7, 8, 5, 87, 88, 45, 87]
my_purify = purify(my_list)
print(my_purify)

#2

what python version are you using? Codecademy uses python2, in which case you should use raw_input()

for python3, use input() (given raw_input() no longer exists in python3)

well, we can’t convert the entire string directly to integer, given there also spaces within the string (or comma’s, depending on what the user entered). So, we need to get all the numbers from the string the user entered, how could we do this? One possible way is to use the split() function, this gives us a list with the numbers (still as string)

then we can loop over the list, and convert to integer (construct a new list with integer values)

note: the user could enter: 1 2 3 or 1, 2, 3 or 1,2,3 If you really want this program to work nicely, you should think about this. Do i want to raise an error when user separate the integers by space? Or should it be able to handle all this scenarios?

Or you can let the user enters the number one by one, and then let them enter q or quit to enter

so many possibilities. See what suits you, just work out one scenario first

if you go for split(), letting the user separate the values by spaces is easiest


#3

Dear kind stetim94, Thank you very much. I am using py 3.4 on my laptop.
I think as you said, 1. I need to take numbers from a user as string by input().
then 2. I must use split() to separate numbers which are still strings. 3. loop through the string numbers and convert them
into int. and then apply other things I want from the numbers.

I am now going to do that to see if can I do and apply the code
Thank you so much dear <3:rose:


#4

the step sound right, so that is good :slight_smile:

take a look at the split documentation:

https://docs.python.org/2/library/stdtypes.html#str.split

so you know the function you are dealing with

after converting them, i would seem wise to append them to a new list (so you have a list of the numbers entered by the user, which are of type int)


#5

Thank you so much for the link dear stetim94. I am going to read the link you provided to me. Yes Exactly I am trying to use a new list which is still a problem and I couldnt solve it yet. I am going to try more. Thank youuuuuu <3


#6

well, let me know if you need help. In that case, post an updated version of your code


#7

Yes dear stetim94. Thank you for your kind support <3


#8

I found that The best way to me to understand and feel a code is to make it to parts in the simplest way I can.
I think This is the code and the method I understand:

my_list = input(“list: “)
splitter = my_list.split(”,”)
print(splitter)

numbers= []
for element in splitter:
number = int(element)
numbers.append(number)
print(numbers)

Now I must apply other things based on this yes?


#9

This is the next step:

my_list = input(“list: “)
splitter = my_list.split(”,”)
print(splitter)

numbers= []
for element in splitter:
number = int(element)
numbers.append(number)
print(numbers)

even =[]
for n in numbers:
if n %2==0:
even.append(n)
n+=1
print(even)


#11

Dear stetim94, Here is the code I have done and I am so happy
Do you have any idea? any suggestions? or do you think this code is ok?
(You are the best teacher ever thank you)

def even_number(x):
    splitter = my_list.split(",")    
    numbers= []
    for element in splitter:
        number = int(element)
        numbers.append(number)

    even =[]
    for n in numbers:
        if n %2==0:
            even.append(n)
            n+=1
    print(even)
        
my_list = input("list: ")
even_number(my_list)

#12

here:

def even_number(x):

you define a function with parameter x, but in the function body you don’t use the parameter anywhere, i would recommend to use the parameter:

splitter = x.split(",")

I like how you break the problem into steps, which is certainly good, but walk over your program after putting everything together, to see you don’t miss things (like an unused function parameter)

I think a print statement to inform the user how they should enter the numbers is quit useful, you know because you wrote the program, but the end user using your program won’t know

Personally, even with the print statement, i wouldn’t trust the users, what if the user entered 1 2 3:

Screenshot_2017-11-15_16-04-26

oh dear, our program crashed :frowning:

There are several things you can do, verify the user input or extract all numbers from the string user entered

But now we are just perfecting the program

one more thing, this line:

n+=1

isn’t doing anything, you can remove it

the for loop will assign the values from the list to the loop iterator, so no need to n += 1


#13

Awwww, dear stetim94, Thank you so much. So I must review my code and Thank you for the complete great explanation. So I am going to test my code again considering the notes you told me now. Thank youuuuuuuuu <3


#14

when I use splitter = x.split(",") the program accept numbers seperated by “, " and wont accept something like 1 2 3,
when i use splitter = my_list.split(” ") so the program wont accept input like 1, 2, 3
so i must do something to handle this error to accept, 123. 1 2 3 or 1,2,3.


#15

the user might even enter a b c, you don’t know

its just, how perfect should your program work? You are going well outside the scope of the exercise now

i think, personally if i wanted to do this i would do:

import re
re.findall(r'\d+', my_list)

this will extract all the integers, however, if the user enters a decimal value (5.2 for example), this will result in two separate values (5 and 2)


#16

Ohhhh import r e??? I never heard that as my base of learning is on order of course on codecademy and so I think i must do more research on each course or maybe codecademy has this course on pro section! I am going to search now about import re and to rewrite my code code based on that dear, Thank you a million times for your kind heart and for your support <3 <redflowe


#17

re (regular expression) isn’t covered anywhere in the course. Python has documentation on it:

https://docs.python.org/3/library/re.html

but regular expressions are pretty tricky


#18

Thank you for providing me the link for re. Dear stetim94 I appreciate it and I am ganna study the link <pinkflower


#19

i would recommend to find a tutorial, the documentation is really dry and really tough to get through

sololearn has RE:

but you need to take the rest of the python course as well.


#20

Awww I am already a member there and I studied some lessons of that but not a complete study there yet. Thank you and are you there ?having any account there to follow you?


#21

i haven’t done anything there in ages: