gpt4 book ai didi

python - NameError:方向未定义?

转载 作者:行者123 更新时间:2023-12-01 07:07:50 26 4
gpt4 key购买 nike

所以我是第一年,有一点编码经验,但不多,我被赋予了制作基于文本的冒险游戏的任务。我的讲师给了我们一个模板并帮助我们开始,但我很困惑为什么这段代码会导致 shell 显示 NameError: name 'direction' is not Defined。有人可以帮助第一年吗?

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):
"""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? ")

关于python - NameError:方向未定义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58359350/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com