-5

ここに私の問題があります。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 関数を呼び出す方法を知っておく必要があります。

何か案は?

4

1 に答える 1

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()(ところで、後にセミコロンは必要ありません)、後でsearch()これを呼び出すと、技術的には、input()()どちらが奇妙に呼び出しているかを意味します。

def search():さらに、ステートメントから何かを移動すると、コードでif呼び出される別の変数を使用searchすることになり、混乱を招くことになります。

于 2013-09-20T18:17:00.177 に答える