raw_input that able to do detect multiple input
Frank
jiewei24 at gmail.com
Sat Apr 6 23:22:42 EDT 2013
Hi Dave,
Sorry for my unclear question.
I didn't use the d = load_friends('friends.csv') now because I'm going use it for other function later on, I should have remove it first to avoid confusion.
This is the code for load_friends , add_info ,display_friends, save_friends function:
def load_friends(filename):
f = open(filename, 'rU')
for row in f:
return list (row.strip() for row in f)
def add_info(new_info, new_list):
# Persons name is the first item of the list
name = new_info[0]
# Check if we already have an item with that name
for item in new_list:
if item[0] == name:
print "%s is already in the list" % name
return False
# Insert the item into the list
new_list.append(new_info)
return True
def display_friends(name, friends_list):
Fname = name[0]
for item in friends_list:
if item[0] == Fname:
print item
break
else:
print False
def save_friends(friend_info, new_list):
with open(friend_info, 'w') as f:
for line in new_list:
f.write(line + '\n')
I will elaborate my question further , when the user type the function call interact() this will appear :
interact()
Friends File: friends.csv
so after which the user would type in the command call maybe we call it " F John Cleese", the program need to know if the user input contain a "f" "a" or "e" at the first char and
if 'f' it mean it would takes a name as an argument, prints out the information about that friend or prints an error message if the given name is notthe name of a friend in the database(friends.csv).
if "a" it would takes four arguments (comma separated) with information
about a person and adds that person as a friend. An error message is printed
if that person is already a friend.
if "e" it would ends the interaction and, if the friends information has been
updated, the information is saved to the friends.csv.
This is the example output
Command: f John Cleese
John Cleese: Ministry of Silly Walks, 5555421, 27 October
Command: f Michael Palin
Unknown friend Michael Palin
Command: f
Invalid Command: f
Command: a Michael Palin
Invalid Command: a Michael Palin
Command: a John Cleese, Cheese Shop, 5552233, 5 May
John Cleese is already a friend
Command: a Michael Palin, Cheese Shop, 5552233, 5 May
Command: f Michael Palin
Michael Palin: Cheese Shop, 5552233, 5 May
Command: e
Saving changes...
Exiting...
So currently I think i had my other functions ready but I do not know how do i apply it into interact()
my rough idea is :
def interact(*arg):
open('friends.csv', 'rU')
d = load_friends('friends.csv')
print "Friends File: friends.csv"
s = raw_input()
command = s.split(" ", 1)
if "f" in command:
# invoke display_friends function
print result
elif "a" in command:
# invoke add_info function
print result
elif "e" in command:
# invoke save_friends function
print result
My idea is to split the user command out to ['f', 'John Cleese'] and use the 'F' to invoke my "f" in the if statement and then i would use the display_friends function to process 'John Cleese' but i'm not sure if i'm able to do it this way
More information about the Python-list
mailing list