Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #42959 > unrolled thread
| Started by | Frank <jiewei24@gmail.com> |
|---|---|
| First post | 2013-04-06 18:03 -0700 |
| Last post | 2013-04-06 20:22 -0700 |
| Articles | 8 — 2 participants |
Back to article view | Back to comp.lang.python
raw_input that able to do detect multiple input Frank <jiewei24@gmail.com> - 2013-04-06 18:03 -0700
Re: raw_input that able to do detect multiple input Dave Angel <davea@davea.name> - 2013-04-06 21:41 -0400
Re: raw_input that able to do detect multiple input Frank <jiewei24@gmail.com> - 2013-04-06 20:22 -0700
Re: raw_input that able to do detect multiple input Dave Angel <davea@davea.name> - 2013-04-07 00:36 -0400
Re: raw_input that able to do detect multiple input Frank <jiewei24@gmail.com> - 2013-04-06 22:00 -0700
Re: raw_input that able to do detect multiple input Dave Angel <davea@davea.name> - 2013-04-07 01:25 -0400
Re: raw_input that able to do detect multiple input Frank <jiewei24@gmail.com> - 2013-04-06 22:00 -0700
Re: raw_input that able to do detect multiple input Frank <jiewei24@gmail.com> - 2013-04-06 20:22 -0700
| From | Frank <jiewei24@gmail.com> |
|---|---|
| Date | 2013-04-06 18:03 -0700 |
| Subject | raw_input that able to do detect multiple input |
| Message-ID | <cb34034e-8cd5-400b-9b71-7d365326d289@googlegroups.com> |
Hi all, I would require advise on this question for function call interact:
the desire outcome:
interact()
Friends File: friends.csv
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...
my code so far for interact:
#interact function
def interact(*arg):
open('friends.csv', 'rU')
d = load_friends('friends.csv')
print "Friends File: friends.csv"
s = raw_input("Please input something: ")
command = s.split(" ", 1)
if "f" in command:
display_friends("command",load_friends('friends.csv'))
print command
#display friend function
def display_friends(name, friends_list):
Fname = name[0]
for item in friends_list:
if item[0] == Fname:
print item
break
else:
print False
Let say if i type in " f John Cleese " and after the line 6 , my value of "command" should be ['f', 'John Cleese']. Is there ways to extract out John Cleese as a input so that i could use it on my function call "display_friends" ?
[toc] | [next] | [standalone]
| From | Dave Angel <davea@davea.name> |
|---|---|
| Date | 2013-04-06 21:41 -0400 |
| Message-ID | <mailman.220.1365298909.3114.python-list@python.org> |
| In reply to | #42959 |
On 04/06/2013 09:03 PM, Frank wrote:
> Hi all, I would require advise on this question for function call interact:
>
> the desire outcome:
> interact()
> Friends File: friends.csv
> 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...
>
> my code so far for interact:
> #interact function
> def interact(*arg):
> open('friends.csv', 'rU')
> d = load_friends('friends.csv')
> print "Friends File: friends.csv"
> s = raw_input("Please input something: ")
> command = s.split(" ", 1)
> if "f" in command:
> display_friends("command",load_friends('friends.csv'))
> print command
>
> #display friend function
> def display_friends(name, friends_list):
> Fname = name[0]
> for item in friends_list:
> if item[0] == Fname:
> print item
> break
> else:
> print False
>
> Let say if i type in " f John Cleese " and after the line 6 , my value of "command" should be ['f', 'John Cleese']. Is there ways to extract out John Cleese as a input so that i could use it on my function call "display_friends" ?
>
>
Nothing about this message makes any sense to me. The function
display_friends() has no body. Code for load_friends() is missing. You
seem to be confusing variable names with literal strings. You open an
input file "friends.csv", but never use the file handle. You store the
return value of load_friends() in d, but never use it. The "desire
outcome" includes lots of stuff that this code won't be producing.
And I cannot understand the question you ask at the end. However, one
thing I see that's wrong is the user is apparently typing a leading and
trailinb blank on the line. If you want to strip out whitespace before
and after, just use strip().
--
DaveA
[toc] | [prev] | [next] | [standalone]
| From | Frank <jiewei24@gmail.com> |
|---|---|
| Date | 2013-04-06 20:22 -0700 |
| Message-ID | <ad79c95a-8cbd-4e6e-bc93-b23e4c11ab94@googlegroups.com> |
| In reply to | #42963 |
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
[toc] | [prev] | [next] | [standalone]
| From | Dave Angel <davea@davea.name> |
|---|---|
| Date | 2013-04-07 00:36 -0400 |
| Message-ID | <mailman.229.1365309396.3114.python-list@python.org> |
| In reply to | #42972 |
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
[toc] | [prev] | [next] | [standalone]
| From | Frank <jiewei24@gmail.com> |
|---|---|
| Date | 2013-04-06 22:00 -0700 |
| Message-ID | <b32166f6-0865-4c96-8350-a46692c4257a@googlegroups.com> |
| In reply to | #42975 |
Now you've saved the data in a different file. How does the next run of the program find it? What user? In what environment can a user enter function calls into your code? -The user will call the function out from IDLE Why is the command invalid? -Because the user need to type out a name after the "f" That's not the way the message is worded in the code - because if user type in " a John Cleese, Cheese Shop, 5552233, 5 May" it mean it would takes four arguments (comma separated) with information about a person and adds that person to my "friends.csv". An error message is printed if that person is already a friend. Because the name "John Cleese" is already in my friends.csv that why it will prompt out "John Cleese is already a friend" In this function and in save_friends, there is no return value, so not clear what you mean by 'result' e ends the interaction and, if the friends information has been updated, the information is saved to the friends.csv , i think i used the wrong function for this. The question I'm told to work on: interact() is the top-level function that denes the text-base user interface as described in the introduction. Here is an example of what is expected from your program. The input is everything after Command: on a line (and the initial friends.csv). Every- thing else is output. Your output should be exactly the same as below for the given input. interact() Friends File: friends.csv 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...
[toc] | [prev] | [next] | [standalone]
| From | Dave Angel <davea@davea.name> |
|---|---|
| Date | 2013-04-07 01:25 -0400 |
| Message-ID | <mailman.232.1365312366.3114.python-list@python.org> |
| In reply to | #42976 |
(You forgot to separate the parts of my comments that you were quoting from your responses. Any decent email program will do that for you automatically, inserting "< " in front of each quoted line. Then you just hit enter a couple of times to type the new stuff right after the part you're quoting.) On 04/07/2013 01:00 AM, Frank wrote: > Now you've saved the data in a different file. How does the next run of > the program find it? > > > What user? In what environment can a user enter function calls into > your code? > -The user will call the function out from IDLE So the user is the programmer. No end-user would be using IDLE to run a program. > > Why is the command invalid? > -Because the user need to type out a name after the "f" But that wouldn't be an invalid command, but invalid data > > That's not the way the message is worded in the code > - because if user type in " a John Cleese, Cheese Shop, 5552233, 5 May" > it mean it would takes four arguments (comma separated) with information > about a person and adds that person to my "friends.csv". An error message is printed if that person is already a friend. Because the name "John Cleese" is already in my friends.csv that why it will prompt out "John Cleese is already a friend" So fix the code, I just pointed out that the message was different. The code says print "%s is already in the list" % name Yet you say the message needs to be: > John Cleese is already a friend One or the other is incorrect. > > In this function and in save_friends, there is no return value, so not > clear what you mean by 'result' > > e ends the interaction and, if the friends information has been > updated, the information is saved to the friends.csv , i think i used the wrong function for this. No, just the wrong filename. I assumed you were going to rename it afterwards, tut apparently not. > > The question I'm told to work on: > interact() is the top-level function that denes the text-base user interface > as described in the introduction. > So if you call interact() in your program at the top-level, then a non-programmer can run the program directly from the terminal window. > Here is an example of what is expected from your program. The input is > everything after Command: on a line (and the initial friends.csv). Every- > thing else is output. Your output should be exactly the same as below for > the given input. > > interact() > Friends File: friends.csv > 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... > You will also need to add an argument to the raw_input() to have it produce the output specified. -- DaveA
[toc] | [prev] | [next] | [standalone]
| From | Frank <jiewei24@gmail.com> |
|---|---|
| Date | 2013-04-06 22:00 -0700 |
| Message-ID | <mailman.230.1365310861.3114.python-list@python.org> |
| In reply to | #42975 |
Now you've saved the data in a different file. How does the next run of the program find it? What user? In what environment can a user enter function calls into your code? -The user will call the function out from IDLE Why is the command invalid? -Because the user need to type out a name after the "f" That's not the way the message is worded in the code - because if user type in " a John Cleese, Cheese Shop, 5552233, 5 May" it mean it would takes four arguments (comma separated) with information about a person and adds that person to my "friends.csv". An error message is printed if that person is already a friend. Because the name "John Cleese" is already in my friends.csv that why it will prompt out "John Cleese is already a friend" In this function and in save_friends, there is no return value, so not clear what you mean by 'result' e ends the interaction and, if the friends information has been updated, the information is saved to the friends.csv , i think i used the wrong function for this. The question I'm told to work on: interact() is the top-level function that denes the text-base user interface as described in the introduction. Here is an example of what is expected from your program. The input is everything after Command: on a line (and the initial friends.csv). Every- thing else is output. Your output should be exactly the same as below for the given input. interact() Friends File: friends.csv 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...
[toc] | [prev] | [next] | [standalone]
| From | Frank <jiewei24@gmail.com> |
|---|---|
| Date | 2013-04-06 20:22 -0700 |
| Message-ID | <mailman.227.1365304971.3114.python-list@python.org> |
| In reply to | #42963 |
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
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web