我需要就这个问题提出建议,这是我必须提出的输出:
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
提前谢谢你们