0

I would require advise on this problem, this is the output that i must come out with :

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

I'm required to come out with a function to do this but i go stuck ,I was thinking if is there anyway to split the user input for example if user type:

f John Cleese

I wonder if I could split the F and John Cleese out as separate input so I could process the output separately. And also is it possible to invoke a function withint a function? this is my code:

def interact(*arg):
    open('friends.csv', 'rU')
    print "Friends File: friends.csv"
    resp = raw_input()
    if "f" in resp:
# display friend function
        display_friends("resp",)
        print "resp"
    elif "a" in resp:
# add friend function
        add_friend("resp",)

my display friends function that i want to invoke within the 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

thank you guys in advance

1 Answer 1

1

First of all, yes, you can place function calls into other functions.

Second of all, if you grab user input, in your example "f John Cleese", then you can simply do what you want with it in your code. For example:

s = raw_input("Please input something: ")
# now I input "f John Cleese", so that is now the value of 's'
# printing the value of 's' will let you see what it is exactly.

command = s.split(' ', 1) 
# the above code will split the string 's' on a ' ' space, 
# and only do it once, and then create a list with the pieces
# so the value of 'command' will be ['f', 'John Cleese'] for your example.

# to access items in the command list use brackets []
command[0] # 'f'
command[1] # 'John Cleese'

With all these tools, which you can consider as advice, I bid you good luck!

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, i got another question ,after i do the command = s.split(' ', 1) how do I take out F from command and use it for other function ? like wise for John Cleese.
I edited my answer, you access them with brackets []. please learn python before asking more questions, these are all basic things, and would save you much time and wasted energy if you just read the basic tutorial, would take you 2 hours at most. There are many tutorials here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.