0

この問題についてアドバイスが必要です。これは、私が出す必要がある出力です:

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

出力を別々に処理できるように、F と John Cleese を別々の入力として分割できないかと思います。また、関数内で関数を呼び出すことは可能ですか? これは私のコードです:

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",)

関数内で呼び出したい表示フレンド関数

def display_friends(name, friends_list):
Fname = name[0]
for item in friends_list:
    if item[0] == Fname:
        print item
        break
    else:
        print False

よろしくお願いします

4

1 に答える 1

1

まず第一に、はい、関数呼び出しを他の関数に配置できます。

次に、「f John Cleese」の例でユーザー入力を取得すると、コードでそれを使用して必要なことを簡単に行うことができます。例えば:

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'

アドバイスとして考えられるこれらすべてのツールを使用して、幸運を祈ります!

于 2013-04-06T09:28:39.470 に答える