raw_input能够检测多个输入

时间:2013-04-06 08:56:42

标签: python python-2.7 raw-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 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

提前谢谢你们

1 个答案:

答案 0 :(得分: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'

使用所有这些工具,你可以考虑作为建议,我祝你好运!