Path: csiph.com!usenet.pasdenom.info!news.albasani.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.005 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'else:': 0.03; 'value,': 0.04; 'elif': 0.05; 'insert': 0.05; 'output': 0.05; 'friend.': 0.07; 'arguments': 0.09; 'debugging.': 0.09; 'executes': 0.09; 'fname': 0.09; 'function:': 0.09; 'question?': 0.09; 'def': 0.12; 'question.': 0.14; '"a"': 0.16; '"e"': 0.16; '"f"': 0.16; '"in"': 0.16; "'\\n')": 0.16; "'w')": 0.16; "['f',": 0.16; 'argument,': 0.16; 'code?': 0.16; 'command:': 0.16; 'item[0]': 0.16; 'list"': 0.16; 'rough': 0.16; 'silly': 0.16; 'skipping': 0.16; 'user?': 0.16; 'wrote:': 0.18; 'split': 0.19; 'later': 0.20; 'command': 0.22; 'input': 0.22; 'example': 0.22; 'print': 0.22; 'header:User- Agent:1': 0.23; 'error': 0.23; 'adds': 0.24; 'char': 0.24; 'header': 0.24; 'file.': 0.24; 'environment': 0.24; 'question': 0.24; 'header:In-Reply-To:1': 0.27; 'idea': 0.28; 'function': 0.29; 'appear': 0.29; 'michael': 0.29; 'on,': 0.29; 'statement': 0.30; "i'm": 0.30; 'code': 0.31; 'file:': 0.31; 'invoke': 0.31; 'once,': 0.31; 'prints': 0.31; 'subject:that': 0.31; 'run': 0.32; 'maybe': 0.34; 'skip:d 20': 0.34; 'but': 0.35; 'there': 0.35; 'really': 0.36; 'data,': 0.36; 'false': 0.36; 'interaction': 0.36; 'next': 0.36; "didn't": 0.36; 'should': 0.36; 'list': 0.37; 'clear': 0.37; 'ends': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'does': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'how': 0.40; 'remove': 0.60; 'break': 0.61; 'name:': 0.61; 'john': 0.61; 'further': 0.61; 'first': 0.61; 'save': 0.62; "you've": 0.63; 'information': 0.63; 'name': 0.63; 'real': 0.63; 'field': 0.63; 'different': 0.65; 'frank': 0.68; 'invalid': 0.68; 'received:74.208': 0.68; 'saving': 0.69; 'friend': 0.79; 'friends': 0.81; "'result'": 0.84; 'confusing': 0.84; 'confusion.': 0.84; 'palin': 0.84; 'received:74.208.4.194': 0.84; 'shop,': 0.84; 'top.': 0.84; 'unclear': 0.84; 'updated,': 0.84; 'ministry': 0.91; 'not:': 0.91; 'besides,': 0.93 Date: Sun, 07 Apr 2013 00:36:14 -0400 From: Dave Angel User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130308 Thunderbird/17.0.4 MIME-Version: 1.0 To: python-list@python.org Subject: Re: raw_input that able to do detect multiple input References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Provags-ID: V02:K0:DgpR2CGaRFTqU7Zer+owXfrG2gNo2b7cF9XcRE+qdpA 5WN93kWUnrDLo51KljtUi+qXjA+hDBYAiHrqNi/wKZ2jbd6vvx dNipUC/b7N2V5dli62xC2uIF9P7VnEXVC3nXPwlsRYYAJVzQBE iQPYxwOiZ0SyfpcyVuTip47TC9KeGgqblDiPoZN9Fy1T3mAIT/ dMYwysw8UqFDqyu1Wf8UfP40OYhjMI9KJaBqm8DPaf23JTpk4e zJ9i6mu0/shYS5GzJbIYA+zlWVNo8yHzGg7beMVov+HQaRLNMp bfXakF/WoXGUw9GRCG61HWqGFUqx0e4ZRm8aoEjacoT0OzFww= = X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 135 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1365309396 news.xs4all.nl 6941 [2001:888:2000:d::a6]:56731 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:42975 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