NameError: направление не определено? - PullRequest
1 голос
/ 13 октября 2019

Итак, я первый год с небольшим опытом программирования, но не очень, и мне дали задание создать текстовую приключенческую игру. Мой лектор дал нам шаблон и, чтобы помочь нам начать, но я не совсем понимаю, почему этот фрагмент кода приводит к тому, что оболочка говорит, что NameError: name 'direction' не определено. Может ли кто-нибудь помочь в первом году?

def print_menu_line(direction, leads_to):
    """This function prints a line of a menu of exits. It takes two strings: a
    direction (the name of an exit) and the name of the room into which it
    leads (leads_to), and should print a menu line in the following format:

    Go <EXIT NAME UPPERCASE> to <where it leads>.

    For example:
    >>> print_menu_line("east", "you personal tutor's office")
    Go EAST to you personal tutor's office.
    >>> print_menu_line("south", "MJ and Simon's room")
    Go SOUTH to MJ and Simon's room."""

    print(rooms[room_livingRoom["exits"]])
    direction = input("Which direction? ")
    leads_to = input("Where do you want to go? ")
    print("Go", direction, "to", leads_to)

    print_menu_line(direction, leads_to)

1 Ответ

1 голос
/ 13 октября 2019

В коде, который вы вставляете, есть ошибки. Вот код, который, как я полагаю, дал вам ваш лектор.

def print_menu_line(direction, leads_to):
    """This function prints a line of a menu of exits. It takes two strings: a
       direction (the name of an exit) and the name of the room into which it
       leads (leads_to), and should print a menu line in the following format:

       Go <EXIT NAME UPPERCASE> to <where it leads>.

     For example:
     >>> print_menu_line("east", "you personal tutor's office")
      Go EAST to you personal tutor's office.
     >>> print_menu_line("south", "MJ and Simon's room")
     Go SOUTH to MJ and Simon's room."""


#print (rooms[room_livingRoom["exits"]])
direction = input("Which direction? ")
leads_to = input("Where do you want to go? ")
print("Go", direction, "to", leads_to)

print_menu_line(direction, leads_to)

Теперь вот решение вашей проблемы:

def print_menu_line(direction, leads_to):
    """ direction and leads_to are the parameters of the function """
    direction = direction.upper() # to have a upper case 
    print("Go", direction, "to", leads_to)


# to get values <direction> and <leads_to>
# direction and lead_to are the arguments you will send to the function
direction = input("Which direction? ")
leads_to = input("Where do you want to go? ")

# call the function
print_menu_line(direction, leads_to)

Обновление: Чтобы понятьРазница между аргументами и параметрами, я переписать код, который вверху:

def print_menu_line(param1, param2):
    """ param1 and param2 are the parameters of the function. From your assignment, you know that param1 refers to the direction and param2 refers to the leads_to variable """

    param1 = param1.upper() # to have a upper case 
    print("Go", param1, "to", param2)


# direction and lead_to are the arguments you will send to the function
direction = input("Which direction? ")
leads_to = input("Where do you want to go? ")
...