所以这是我的问题。我在python 3.3.2中创建了一个电话簿,我需要知道如何调用单独的函数。这是我的代码:
details = {"Toby": "123", "James": "234", "Paul": "345"}
print("Welcome To The Telephone Directory.\n")
print("To search out specific details, please type Search.\n To add a new person to the Directory, type Add.\n To change someone's details, please type Edit.")
search = input();
if(input() == search.strip() in "search Search SEARCH".split()):
def search():
print("Please enter the name of the customer.")
customer = input()
print("Number:" ,details.get(customer))
if(customer not in details):
print("I'm sorry, but the person you defined is not in the directory.")
flag = True
while flag:
search()
flag = input("Would you like to look up another person? [Y/N]") == "y"
if(input() == add.strip() in "add Add ADD".split()):
def add():
print("Please enter the name of the new entry.")
name = input()
print("Now enter the entry's phone number.")
telNumber = input()
details[name] = telNumber
print("You have succesfully added a new entry:\n",name,"\n",telNumber)
所以我需要知道如何从一开始就调用Add函数。
有什么想法吗?
答案 0 :(得分:1)
您正在定义从未实际调用的If语句中的函数
你想要这样的东西:
def do_definition(x): #defining your function, before you call it
print(x)
y = 'foo'
if statement:
do_definition(y) # this is actually calling the function
通常在您开始时,您希望首先在顶部定义您的功能,然后在其他地方调用它们
我认为你可能想检查你的语法。
当你正在制作search = input()
时(你在btw之后不需要一个半冒号),然后你再来电话search()
这在技术上意味着你正在调用input()()
这很奇怪。
另外,如果您将def search():
内容移出if
语句,则表示您在代码中使用了另一个名为search
的变量,这会导致生活混乱。