Python mange with liste

B

Bala Ji

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()
 
R

Rustom Mody

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
 
R

Rustom Mody

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

Well it may be better to write that as
names = [("nam1", "F", True), ("nam2", "M", False), etc]

ie represent Y by boolean True rather than the string "Y" etc

though you could get bitten by the fact that python booleans are
strange in that True is True but all kinds of things are True-ish
 
P

Piet van Oostrum

Bala Ji said:
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

What does the O mean here?
 
B

Bala Ji

Oh sorry it's a Y (in french it's O) sorry for the mistake


Le dimanche 29 décembre 2013 00:30:23 UTC+1, Bala Ji a écrit :
 
B

Bala Ji

hello,

thank you for your help

i wrote this:

x="nam1"
y="F"

names = [("nam1", "F", "Y"), ("nam2", "M", "N")]
l = len(names)
for i in range(0,l):
print names[0]
print names[1]
if x == names[0] and y == names[1]:
message = "right"
else:
message = "wrong"

print message


normally it must tell me "right" but it tells me "wrong"

best
 
R

Rustom Mody

hello,

thank you for your help

i wrote this:

x="nam1"
y="F"

names = [("nam1", "F", "Y"), ("nam2", "M", "N")]
l = len(names)
for i in range(0,l):
print names[0]
print names[1]
if x == names[0] and y == names[1]:
message = "right"
else:
message = "wrong"

print message


Ok lets start with

1.

l = len(names)
for i in range(0,l): ... names[1] ...

Better to do in python as

for n in names: ... n ...
ie use n where you were using names
no need for range, len, indexing etc etc

2.
You are setting (ie assigning) message each time round the loop
Try writing a function that does NO set (assign) NO print

Ok this time let me write it for you
So I write a function called foo, taking an argument called nn.... for n in names:
.... if n == nn: return True
.... return False
....

Note: No input, No output, No file IO and above all No assignment
False

Notice my foo takes an argument that is a triplet
You need to change foo to taking name and sex and ignoring the third element

Your homework -- Oui??
 
F

Frank Millman

Bala Ji said:
hello,

thank you for your help

i wrote this:

x="nam1"
y="F"

names = [("nam1", "F", "Y"), ("nam2", "M", "N")]
l = len(names)
for i in range(0,l):
print names[0]
print names[1]
if x == names[0] and y == names[1]:
message = "right"
else:
message = "wrong"

print message


normally it must tell me "right" but it tells me "wrong"

best


Your problem is that, after you find a valid name, you continue looping, and
then find an invalid name, so the message is over-written.

The usual way to terminate a loop without continuing to the next item is
with the 'break' statement.

Here are three variations of your code, each with an improvement over the
previous one -

1. This adds the break statement - it should do what you want -
l = len(names)
for i in range(0,l):
print names[0]
print names[1]
if x == names[0] and y == names[1]:
message = "right"
break
else:
message = "wrong"

2. This uses a feature of python which specifies an action to be taken only
if the loop continues to the end without interruption -
l = len(names)
for i in range(0,l):
print names[0]
print names[1]
if x == names[0] and y == names[1]:
message = "right"
break
else:
message = "wrong"

Note that the last two lines are indented to line up with the 'for '
statement. In the previous version, message is set to 'wrong' for every
iteration of the loop until a valid name is found. In this version, it is
only set to 'wrong' if no valid name is found.

3. This uses a feature of python which allows you to iterate over the
contents of a list directly -
for name in names:
print name[0]
print name[1]
if x == name[0] and y == name[1]:
message = "right"
break
else:
message = "wrong"

Hope this gives you some ideas.

Frank Millman
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top