如何在python中调用已定义的函数?

时间:2019-01-02 11:43:56

标签: python

  

尽管我访问了多个网站,但在这里找不到我在此代码中使用过的确切示例。从def msg(room):直到游戏结束图像是我定义的函数。在最底端是我调用所有函数的地方,但是我不确定如何调用此特定函数。

def msg(room):
    if room['msg']== '': #There is no custom message
        return "You have entered the " + room['name']+'.'
    else:
        return room ['msg']

def getChoice(room,dir):
    if dir == 'N':
        choice = 0
    elif dir =='E':
        choice = 1
    elif dir=='S':
        choice =2
    elif dir == 'W':
        choice = 3
    else:
        return -1

    if room['directions'][choice] == 0:
        return 4
    else:
        return choice

if pointsUser == "1":
    print ("You encounter a sunken home, and your metal detector tells you there is something inside.")
    print ("You have found phosphorus, but now you must collect other elements.")
    print ("You must use your keys to navigate around the rooms to find the elements hidden insdie.")
    dirs = (0,0,0,0) #default NESW

    entrance = {'name':'Entrance Way','directions':dirs,'msg':''} #dictionary is a collection of names and properties, keys and values. Each dictionay here has 3 properties. 
    livingroom = {'name':'Living room','directions':dirs,'msg':''}
    thethehallway = {'name':'Hallway','directions':dirs,'msg':''}
    kitchen = {'name':'kitchen','directions':dirs,'msg':''}
    diningroom = {'name':'Dining room','directions':dirs,'msg':''}
    familyroom = {'name':'Family room','directions':dirs,'msg':''}
      #directions are tuples: Rooms to the (N, E, S, W)
    entrance['directions'] = (kitchen,livingroom,0,0)
    livingroom['directions'] = (diningroom,0,0,entrance,)
    hallway ['directions']=(0,kitchen,familyroom)
    kitchen ['directions'] = (0,diningroom,entrance,hallway)
    diningroom ['directions'] = (0,0,livingroom,kitchen)
    familyroom['directions']=(0,hallway,0,0)

    #rooms where the elements may be
    rooms = (livingroom,hallway,kitchen,diningroom,familyroom)
    rooms_with_eggs= random.choice(rooms)
    elemDelivered = False
    room = entrance
    print ("Welcome, Adventurer! Can you find the precious elements?")

    while True: #nested while loop 
        if elemDelivered and room['name'] == 'Entrance Way':
            print ("You've picked up the elements and returned")
            print ("To the entrance of the sunken home. You can now swin away,")
            print("congratulations!")
            break;
        elif not elemDelivered and room['name'] == room_with_elem['name']:
               elemDelivered = True
               print (msg(room) + "There are the elements with dark figure")
               print("in the corner... You have found your elements, now swim away quickly!")
        else:
               print (msg(room))
               room ['msg'] = "You are back in the" + room['name']

               stuck =  True
               while stuck:
                   dir = input("Which direction do you want to  go: N,E,S or W?")
                   choice = get_choice(room,dir)
                   if choice == -1:
                       print ("Please enter N, E, S, or W?")
                   elif choice == 4:
                       print("You cannot go in that direction.")
                   else:
                       room = room ['directions'][choice]
                       stuck = False

else:
    print("Game over")

2 个答案:

答案 0 :(得分:2)

考虑一个简单的例子, 首先定义一个这样的函数,

def showMsg(myMsg):
    print(myMsg)

现在,像这样调用上面定义的函数

showMsg('My Message!')

输出:My Message!

在您的代码中,您可以像这样调用msg函数,

room = {msg: 'my msg', name: 'xyz'}
msg(room)    # make a dictionary of name room = {} and pass to this function

房间只是一个例子而已。

答案 1 :(得分:0)

要创建可以定义然后调用的函数,必须创建一个名为main()的主函数。然后,在if语句之后调用此函数,如下所示:

如果....:#这应满足您需要满足的条件 main()#您可以执行此操作以调用函数并开始编写代码

缩进和NameErrors还有其他错误,这些错误在代码可以运行之前已得到解决,但是我之前所说的是解决方案。