Python mange with liste

Rustom Mody rustompmody at gmail.com
Sat Dec 28 22:08:46 EST 2013


On Sun, Dec 29, 2013 at 5:00 AM, Bala Ji wrote:
> Hello guys,
> i need some help with is program
>
> I have a txt file "test.txt" where there is Name;Sexe;Answer(Y or N)
> example of txt file:
> --------------------------------------
> nam1;F;Y
> nam2;M;N
> nam3;F;Y
> nam4;M;N
> halo;M;Y
> rock;M;N
> nam1;F;N
> _________________________________
>
> so my program will ask the name, sexe, and answer and it will tell me if the name exist or not
>
> example i will enter
> nam1;F;O
>
> The program must tell me that nam1 with sexe F existe. (it must take in count the answer)
>
> ------------------------------------------------
> name = raw_input('name: ')
> sexe = raw_input('sexe: ')
> r1 = raw_input('r1 Y or N: ')
> infos = name+";"+sexe+";"+r1
>
> f=open("test.txt","r")
> conten = f.read()
> print conten
> f.close()
>
>         #f=open("test.txt","a")
>         #f.write(infos)
>         #f.write('\n')
>         #f.close()
> ------------------------------------------------

One general rule of programming is:
If you dont know how to solve a problem:
- solve a related simpler problem
- figure out how to convert your original problem into the simpler one


So heres a simpler problem to try:

Give up on file-IO, ie dont use the EXTERNAL file

nam1;F;Y
nam2;M;N
nam3;F;Y
nam4;M;N
halo;M;Y
rock;M;N
nam1;F;N

But ASSUME you have the internal python data structure
names = [("nam1", "F", "Y"), ("nam2", "M", "N")] # complete the list

Likewise if you wish, get rid of the raw_inputs and just ASSUME you
somehow have a tuple: a variable info of the form
(name, sex, r1) # whatever r1 is

Now:
1. solve your problem for the INTERNAL data structures:
    a. names which is a list
    b. info which is like an element of that list

Note: Observe how I sneakily made info look like the things in names

2. Convert your problem into the above by using suitable file-IO

3. If there is something in the above you dont understand -- whether
python or English -- write back here

Best
Rusi


-- 
http://blog.languager.org



More information about the Python-list mailing list