当我试着运行这个程序时,我得到了这个错误:File";python“;,第33行,in<;模块>;名称错误:全局名称';惠普';没有定义
我不知道为什么这不起作用;它出现了错误:当我试着运行这个程序时,我得到了这个错误:File";python“;,第33行,in<;模块>;名称错误:全局名称';惠普';没有定义,python,Python,我不知道为什么这不起作用;它出现了错误: 文件“python”,第33行,在NameError中:未定义全局名称“hp” def handle_room(location): global hp if location == "start": print("You are standing on a path at the edge of a jungle. There is a cave to your left and a beach to your rig
文件“python”,第33行,在NameError中:未定义全局名称“hp”
def handle_room(location):
global hp
if location == "start":
print("You are standing on a path at the edge of a jungle. There is a cave to your left and a beach to your right.")
direction = raw_input("Do you want to go left or right?")
if direction == "left":
return "cave"
elif direction == "right":
return "beach"
elif location == "cave":
print("You walk to the cave and notice there is an opening.")
print("A small snake bites you, and you lose 20 health points.")
hp = hp - 20
answer = raw_input("Do you want to go deeper?")
if answer == "yes":
return "deep_cave"
else:
return "start"
elif location == "beach":
print("You walk to the beach but remember you do not have any swimwear.")
print("The cool water revitalizes you. You have never felt more alive, gain 70 health points.")
hp += 70
return "end"
else:
print("Programmer error, room ", location, " is unknown")
return "end"
location = "start"
while location != "end":
location = handle_room(location)
print("You now have ", hp, "health points.")
if hp <= 0:
print("You are dead. I am sorry.")
print("Your adventure has ended. Goodbye.")
def手柄室(位置):
全球惠普
如果位置==“开始”:
打印(“你站在丛林边缘的一条小路上,左边是一个洞穴,右边是一个海滩。”)
方向=原始输入(“您想向左走还是向右走?”)
如果方向==“左”:
返回“洞穴”
elif方向==“右”:
返回“海滩”
elif位置==“洞穴”:
打印(“你走到洞穴,发现有一个洞口。”)
打印(“一条小蛇咬了你,你会失去20点生命值。”)
hp=hp-20
回答=原始输入(“您想更深入吗?”)
如果回答=“是”:
返回“深谷洞”
其他:
返回“开始”
elif位置==“海滩”:
打印(“你走到海滩,但记住你没有泳衣。”)
打印(“清凉的水使你恢复活力。你从未感到如此有活力,获得70点生命值。”)
hp+=70
返回“结束”
其他:
打印(“编程器错误,房间”,位置,“未知”)
返回“结束”
location=“开始”
而位置!=“结束”:
位置=手柄室(位置)
打印(“您现在有”,hp,“健康积分”)
如果hp只需在下一行添加hp=0
location = "start"
def handle_room(location):
global hp
hp=0 # assigned a value to global variable
if location == "start":
print("You are standing on a path at the edge of a jungle. There is a cave to your left and a beach to your right.")
direction = raw_input("Do you want to go left or right?")
if direction == "left":
return "cave"
elif direction == "right":
return "beach"
elif location == "cave":
print("You walk to the cave and notice there is an opening.")
print("A small snake bites you, and you lose 20 health points.")
hp = hp - 20
answer = raw_input("Do you want to go deeper?")
if answer == "yes":
return "deep_cave"
else:
return "start"
elif location == "beach":
print("You walk to the beach but remember you do not have any swimwear.")
print("The cool water revitalizes you. You have never felt more alive, gain 70 health points.")
hp += 70
return "end"
else:
print("Programmer error, room ", location, " is unknown")
return "end"
location = "start"
while location != "end":
location = handle_room(location)
print("You now have ", hp, "health points.")
if hp <= 0:
print("You are dead. I am sorry.")
break
print("Your adventure has ended. Goodbye.")
location=“开始”
def手柄室(位置):
全球惠普
hp=0#为全局变量赋值
如果位置==“开始”:
打印(“你站在丛林边缘的一条小路上,左边是一个洞穴,右边是一个海滩。”)
方向=原始输入(“您想向左走还是向右走?”)
如果方向==“左”:
返回“洞穴”
elif方向==“右”:
返回“海滩”
elif位置==“洞穴”:
打印(“你走到洞穴,发现有一个洞口。”)
打印(“一条小蛇咬了你,你会失去20点生命值。”)
hp=hp-20
回答=原始输入(“您想更深入吗?”)
如果回答=“是”:
返回“深谷洞”
其他:
返回“开始”
elif位置==“海滩”:
打印(“你走到海滩,但记住你没有泳衣。”)
打印(“清凉的水使你恢复活力。你从未感到如此有活力,获得70点生命值。”)
hp+=70
返回“结束”
其他:
打印(“编程器错误,房间”,位置,“未知”)
返回“结束”
location=“开始”
而位置!=“结束”:
位置=手柄室(位置)
打印(“您现在有”,hp,“健康积分”)
如果hp只需在下一行添加hp=0
location = "start"
def handle_room(location):
global hp
hp=0 # assigned a value to global variable
if location == "start":
print("You are standing on a path at the edge of a jungle. There is a cave to your left and a beach to your right.")
direction = raw_input("Do you want to go left or right?")
if direction == "left":
return "cave"
elif direction == "right":
return "beach"
elif location == "cave":
print("You walk to the cave and notice there is an opening.")
print("A small snake bites you, and you lose 20 health points.")
hp = hp - 20
answer = raw_input("Do you want to go deeper?")
if answer == "yes":
return "deep_cave"
else:
return "start"
elif location == "beach":
print("You walk to the beach but remember you do not have any swimwear.")
print("The cool water revitalizes you. You have never felt more alive, gain 70 health points.")
hp += 70
return "end"
else:
print("Programmer error, room ", location, " is unknown")
return "end"
location = "start"
while location != "end":
location = handle_room(location)
print("You now have ", hp, "health points.")
if hp <= 0:
print("You are dead. I am sorry.")
break
print("Your adventure has ended. Goodbye.")
location=“开始”
def手柄室(位置):
全球惠普
hp=0#为全局变量赋值
如果位置==“开始”:
打印(“你站在丛林边缘的一条小路上,左边是一个洞穴,右边是一个海滩。”)
方向=原始输入(“您想向左走还是向右走?”)
如果方向==“左”:
返回“洞穴”
elif方向==“右”:
返回“海滩”
elif位置==“洞穴”:
打印(“你走到洞穴,发现有一个洞口。”)
打印(“一条小蛇咬了你,你会失去20点生命值。”)
hp=hp-20
回答=原始输入(“您想更深入吗?”)
如果回答=“是”:
返回“深谷洞”
其他:
返回“开始”
elif位置==“海滩”:
打印(“你走到海滩,但记住你没有泳衣。”)
打印(“清凉的水使你恢复活力。你从未感到如此有活力,获得70点生命值。”)
hp+=70
返回“结束”
其他:
打印(“编程器错误,房间”,位置,“未知”)
返回“结束”
location=“开始”
而位置!=“结束”:
位置=手柄室(位置)
打印(“您现在有”,hp,“健康积分”)
如果hpLike错误表示未定义变量hp
。只需在文件顶部添加一个声明
hp = 100 #
Like error表示未定义变量hp
。只需在文件顶部添加一个声明
hp = 100 #
您尚未初始化hp
这项工作:
hp = 19
location = "start"
while location != "end":
location = handle_room(location)
print("You now have ", hp, "health points.")
if hp <= 0:
print("You are dead. I am sorry.")
break
print("Your adventure has ended. Goodbye.")
hp=19
location=“开始”
而位置!=“结束”:
位置=手柄室(位置)
打印(“您现在有”,hp,“健康积分”)
如果hp尚未初始化hp
这项工作:
hp = 19
location = "start"
while location != "end":
location = handle_room(location)
print("You now have ", hp, "health points.")
if hp <= 0:
print("You are dead. I am sorry.")
break
print("Your adventure has ended. Goodbye.")
hp=19
location=“开始”
而位置!=“结束”:
位置=手柄室(位置)
打印(“您现在有”,hp,“健康积分”)
如果hp