raw_input that able to do detect multiple input

F

Frank

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" ?
 
D

Dave Angel

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().
 
F

Frank

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
 
F

Frank

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
 
D

Dave Angel

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

Frank

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 de nes 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...
 
F

Frank

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 de nes 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...
 
D

Dave Angel

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


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 de nes 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.
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top