raw_input that able to do detect multiple input

Dave Angel davea at davea.name
Sun Apr 7 00:36:14 EDT 2013


On 04/06/2013 11:22 PM, Frank wrote:
> 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)

This is a mighty confusing way of skipping the first line.  You make it 
look like a loop, but it only executes once, since you have a return 
inside.  Besides, when you save the data, you don't put an extra header 
line at the top.  So it's not consistent.

>
> 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')

Now you've saved the data in a different file.  How does the next run of 
the program find it?

>
>
> I will elaborate my question further  , when the user type the function call interact()

What user?  In what environment can a user enter function calls into 
your code?

> 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

Why is the command invalid?

> Command: a Michael Palin
> Invalid Command: a Michael Palin
> Command: a John Cleese, Cheese Shop, 5552233, 5 May
> John Cleese is already a friend

That's not the way the message is worded in the code

> 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:

You don't really want "in" here.  You just want the first field to match 
"f"  So why not:
        if "f" == command[0]:

>          # invoke display_friends function

In this function and in save_friends, there is no return value, so not 
clear what you mean by  'result'

>          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
>
>

It's all over but the debugging.  What's the real question?


-- 
DaveA



More information about the Python-list mailing list