'NameError: name is not defined' when trying to modify a global variable
'NameError: name is not defined' when trying to modify a global variable
我在尝试使用函数修改脚本中的变量时遇到问题。
def damage(x):
""" This function will determine the amount of damage you will take """
""" hit_c is the chance the enemy has to hit you """
from random import randint
global User_HP
global Enemy_HP
hit_c = randint(1,5)
User_damage = randint(1,4)
if hit_c >= 2:
Enemy_HP -= User_damage
lcd_print(textscroll(f"You dealt {User_damage} damage!"))
lcd_clear()
lcd_print(textscroll(f"The enemy has {Enemy_HP} HP")
sleep(1)
elif hit_c == 1:
lcd_print("You missed!")
if Enemy_HP <= 0:
pass
else:
hit_c = randint(1,5)
Enemy_damage = randint(1,3)
if hit_c >= 2:
User_HP -= Enemy_damage
lcd_print(textscroll(f"You took {Enemy_damage} damage!"))
lcd_clear()
lcd_print(User_HP)
elif hit_c == 1:
lcd_print(textscroll("The enemy missed!"))
当我试图在这一行修改它时,函数不会读取全局变量Enemy_HP。
Enemy_HP -= User_damage
注意,我在脚本的开头定义了 Enemy_HP = 10。
非常感谢您的帮助!
错误-
File "D:\Desktop\Python Scripts\Lab Project\Lab_Project_Functions.py", line 41, in damage
Enemy_HP -= User_damage
NameError: name 'Enemy_HP' is not defined`
编辑:
这是完整的代码,很抱歉之前没有包含这个,我还导入了几个函数
from time import sleep
from random import randint
from operator import itemgetter
from engi1020.arduino import *
User_HP = 10
Enemy_HP = 10
wait_1 = True
def loading(wait_time):
if wait_1 == True:
sleep(1)
lcd_clear()
lcd_print('.')
sleep(1)
lcd_clear()
lcd_print('..')
sleep(1)
lcd_clear()
lcd_print('...')
sleep(1)
lcd_clear()
lcd_print('.')
sleep(1)
lcd_clear()
lcd_print('..')
sleep(1)
lcd_clear()
lcd_print('...')
sleep(1)
lcd_clear()
inventory = []
from Lab_Project_Functions import *
#Get some basic gameplay information
lcd_print("starting the game")
lcd_clear()
loading(wait_1)
lcd_print(textscroll("Hello!, What speed would you like the dialogue to play at? ('slow, normal, fast')"))
Dia_Spd = input()
if Dia_Spd == "slow":
DiaSpd = 4
elif Dia_Spd == "normal":
DiaSpd = 3
elif Dia_Spd == "fast":
DiaSpd = 2
elif Dia_Spd == "test":
DiaSpd = 0
lcd_clear()
lcd_print()
loading(wait_1)
'''
name = input("What is your name?")
class_ = "Knight"
sleep(1)
class_ = input("Which class is befitting you young adventurer?"
" A noble 'Knight'?"
" A Studious 'Wizard'?"
" Or a Mystifying 'Warlock'?")
'''
loading(wait_1)
#lcd_print(f"What ho {class_} {name}, welcome to the game!!")
lcd_print(textscroll("What ho brave knight, welcome to the game!!"))
sleep(DiaSpd)
lcd_clear()
lcd_print(textscroll("Let's start with a some basics"))
sleep(DiaSpd)
lcd_clear()
#Start by creating a basic combat system.
lcd_print(textscroll("An enemy approaches you! Learn to fight!"))
sleep(DiaSpd)
lcd_clear()
#Create more combat elements.
while User_HP > 0:
lcd_print(textscroll("Pick a fighting option."))
lcd_print("FIGHT--ITEM--DEF")
fight_option = input()
if fight_option == 'FIGHT' or 'fight' or 'Fight':
damage(1)
elif fight_option == 'ITEM' or 'item' or 'Item':
item_pickup(1)
else:
defend(1)
if Enemy_HP <= 0:
lcd_print(textscroll("THE ENEMY WAS SLAIN!"))
break
sleep(DiaSpd)
lcd_print(textscroll("Good job you're learning the basics!"))
sleep(DiaSpd)
lcd_print(textscroll("To reward you for your efforts, here is an item that the enemy dropped!"))
sleep(DiaSpd)
item_pickup(1)
#remember to redefine enemy hp before next fight
lcd_print(textscroll("Hi ho yee noble knight, welcome to your humble abode. You can perform all sorts of actions here."))
sleep(DiaSpd)
lcd_clear()
lcd_print(textscroll("What would you like to do?"))
sleep(DiaSpd)
lcd_clear()
lcd_print(textscroll("Look through inventory----Rest (Heal)----Go adventuring"))
home_option = input() #change this to accept input from button & joystick
while True:
if home_option == 1:
pass
if home_option == 2:
pass
if home_option == 3:
pass
函数导入来自这里
from engi1020.arduino import *
#Looting // Item Pickup Function def item_pickup(enemy_num):
""" This Function will determine the items the user picks up """
'''
enemy_num is the variable deciding which enemy you are fighting
'''
from random import randint
rand_item = randint(1,4)
global inventory
if enemy_num == 1:
if rand_item <= 3:
inventory.append('IRON_SWORD')
print(textscroll("You picked up the IRON SWORD!"))
elif rand_item == 4:
inventory.append('STEELY_VENGANCE')
lcd_print(textscroll("You found a rare item! 'STEELY VENGANCE' was added to your inventory!"))
else:
'no item found'
if enemy_num == 2:
pass
#Combat Function def damage(x):
""" This function will determine the amount of damage you will take """
""" hit_c is the chance the enemy has to hit you """
from random import randint
global User_HP
global Enemy_HP
hit_c = randint(1,5)
User_damage = randint(1,4)
if hit_c >= 2:
Enemy_HP -= User_damage
lcd_print(textscroll(f"You dealt {User_damage} damage!"))
lcd_clear()
lcd_print(textscroll(f"The enemy has {Enemy_HP} HP")
sleep(1)
elif hit_c == 1:
lcd_print("You missed!")
if Enemy_HP <= 0:
pass
else:
hit_c = randint(1,5)
Enemy_damage = randint(1,3)
if hit_c >= 2:
User_HP -= Enemy_damage
lcd_print(textscroll(f"You took {Enemy_damage} damage!"))
lcd_clear()
lcd_print(User_HP)
elif hit_c == 1:
lcd_print(textscroll("The enemy missed!"))
#Item Select Function def item_select(item_number):
from operator import itemgetter
while True:
lcd_print(textscroll("What item would you like to use?"))
lcd_clear()
sleep(1)
lcd_print(textscroll("note you need to select items numerically, i.e. 0, 1, 2, 3,...,etc. "))
lcd_clear()
sleep(1)
lcd_print(textscroll(inventory))
item_choice = int(input())
lcd_clear()
lcd_print("Which item?:")
num_items = len(inventory)
if item_choice < num_items:
item = itemgetter(item_choice)(inventory)
if item == potion:
global User_HP
User_HP += 5
lcd_print(User_HP)
break
else:
lcd_print(textscroll("ERROR! ITEM CHOICE IS AN INVALID OPTION!"))
#Defend Function def defend(User):
Enemy_damage = randint(1,3)
lcd_print(textscroll(f"The user blocked {Enemy_damage} damage"))
#Text Scrolling Function def textscroll(text):
from time import sleep
i = 0
j = 15
while True:
lcd_print(text[i:j])
i += 1
j += 1
sleep(0.2)
lcd_clear()
if j >= len(text) + 15:
break
你的错误是因为你没有给它赋值(即使你说了):
exception NameError: Raised when a local or global name is not found. This applies only to unqualified names. The associated value is an error message that includes the name that could not be found.
来源:https://docs.python.org/3/library/exceptions.html#bltin-exceptions
您的代码中的问题是您试图从一个文件访问变量而不导入它们。
全局变量只存在于它们定义的文件中。
像导入函数一样导入它们:
import Enemy_HP
我在尝试使用函数修改脚本中的变量时遇到问题。
def damage(x):
""" This function will determine the amount of damage you will take """
""" hit_c is the chance the enemy has to hit you """
from random import randint
global User_HP
global Enemy_HP
hit_c = randint(1,5)
User_damage = randint(1,4)
if hit_c >= 2:
Enemy_HP -= User_damage
lcd_print(textscroll(f"You dealt {User_damage} damage!"))
lcd_clear()
lcd_print(textscroll(f"The enemy has {Enemy_HP} HP")
sleep(1)
elif hit_c == 1:
lcd_print("You missed!")
if Enemy_HP <= 0:
pass
else:
hit_c = randint(1,5)
Enemy_damage = randint(1,3)
if hit_c >= 2:
User_HP -= Enemy_damage
lcd_print(textscroll(f"You took {Enemy_damage} damage!"))
lcd_clear()
lcd_print(User_HP)
elif hit_c == 1:
lcd_print(textscroll("The enemy missed!"))
当我试图在这一行修改它时,函数不会读取全局变量Enemy_HP。
Enemy_HP -= User_damage
注意,我在脚本的开头定义了 Enemy_HP = 10。
非常感谢您的帮助!
错误-
File "D:\Desktop\Python Scripts\Lab Project\Lab_Project_Functions.py", line 41, in damage
Enemy_HP -= User_damage
NameError: name 'Enemy_HP' is not defined`
编辑: 这是完整的代码,很抱歉之前没有包含这个,我还导入了几个函数
from time import sleep from random import randint from operator import itemgetter from engi1020.arduino import * User_HP = 10 Enemy_HP = 10 wait_1 = True def loading(wait_time): if wait_1 == True: sleep(1) lcd_clear() lcd_print('.') sleep(1) lcd_clear() lcd_print('..') sleep(1) lcd_clear() lcd_print('...') sleep(1) lcd_clear() lcd_print('.') sleep(1) lcd_clear() lcd_print('..') sleep(1) lcd_clear() lcd_print('...') sleep(1) lcd_clear() inventory = [] from Lab_Project_Functions import * #Get some basic gameplay information lcd_print("starting the game") lcd_clear() loading(wait_1) lcd_print(textscroll("Hello!, What speed would you like the dialogue to play at? ('slow, normal, fast')")) Dia_Spd = input() if Dia_Spd == "slow": DiaSpd = 4 elif Dia_Spd == "normal": DiaSpd = 3 elif Dia_Spd == "fast": DiaSpd = 2 elif Dia_Spd == "test": DiaSpd = 0 lcd_clear() lcd_print() loading(wait_1) ''' name = input("What is your name?") class_ = "Knight" sleep(1) class_ = input("Which class is befitting you young adventurer?" " A noble 'Knight'?" " A Studious 'Wizard'?" " Or a Mystifying 'Warlock'?") ''' loading(wait_1) #lcd_print(f"What ho {class_} {name}, welcome to the game!!") lcd_print(textscroll("What ho brave knight, welcome to the game!!")) sleep(DiaSpd) lcd_clear() lcd_print(textscroll("Let's start with a some basics")) sleep(DiaSpd) lcd_clear() #Start by creating a basic combat system. lcd_print(textscroll("An enemy approaches you! Learn to fight!")) sleep(DiaSpd) lcd_clear() #Create more combat elements. while User_HP > 0: lcd_print(textscroll("Pick a fighting option.")) lcd_print("FIGHT--ITEM--DEF") fight_option = input() if fight_option == 'FIGHT' or 'fight' or 'Fight': damage(1) elif fight_option == 'ITEM' or 'item' or 'Item': item_pickup(1) else: defend(1) if Enemy_HP <= 0: lcd_print(textscroll("THE ENEMY WAS SLAIN!")) break sleep(DiaSpd) lcd_print(textscroll("Good job you're learning the basics!")) sleep(DiaSpd) lcd_print(textscroll("To reward you for your efforts, here is an item that the enemy dropped!")) sleep(DiaSpd) item_pickup(1) #remember to redefine enemy hp before next fight lcd_print(textscroll("Hi ho yee noble knight, welcome to your humble abode. You can perform all sorts of actions here.")) sleep(DiaSpd) lcd_clear() lcd_print(textscroll("What would you like to do?")) sleep(DiaSpd) lcd_clear() lcd_print(textscroll("Look through inventory----Rest (Heal)----Go adventuring")) home_option = input() #change this to accept input from button & joystick while True: if home_option == 1: pass if home_option == 2: pass if home_option == 3: pass
函数导入来自这里
from engi1020.arduino import *
#Looting // Item Pickup Function def item_pickup(enemy_num):
""" This Function will determine the items the user picks up """
'''
enemy_num is the variable deciding which enemy you are fighting
'''
from random import randint
rand_item = randint(1,4)
global inventory
if enemy_num == 1:
if rand_item <= 3:
inventory.append('IRON_SWORD')
print(textscroll("You picked up the IRON SWORD!"))
elif rand_item == 4:
inventory.append('STEELY_VENGANCE')
lcd_print(textscroll("You found a rare item! 'STEELY VENGANCE' was added to your inventory!"))
else:
'no item found'
if enemy_num == 2:
pass
#Combat Function def damage(x):
""" This function will determine the amount of damage you will take """
""" hit_c is the chance the enemy has to hit you """
from random import randint
global User_HP
global Enemy_HP
hit_c = randint(1,5)
User_damage = randint(1,4)
if hit_c >= 2:
Enemy_HP -= User_damage
lcd_print(textscroll(f"You dealt {User_damage} damage!"))
lcd_clear()
lcd_print(textscroll(f"The enemy has {Enemy_HP} HP")
sleep(1)
elif hit_c == 1:
lcd_print("You missed!")
if Enemy_HP <= 0:
pass
else:
hit_c = randint(1,5)
Enemy_damage = randint(1,3)
if hit_c >= 2:
User_HP -= Enemy_damage
lcd_print(textscroll(f"You took {Enemy_damage} damage!"))
lcd_clear()
lcd_print(User_HP)
elif hit_c == 1:
lcd_print(textscroll("The enemy missed!"))
#Item Select Function def item_select(item_number):
from operator import itemgetter
while True:
lcd_print(textscroll("What item would you like to use?"))
lcd_clear()
sleep(1)
lcd_print(textscroll("note you need to select items numerically, i.e. 0, 1, 2, 3,...,etc. "))
lcd_clear()
sleep(1)
lcd_print(textscroll(inventory))
item_choice = int(input())
lcd_clear()
lcd_print("Which item?:")
num_items = len(inventory)
if item_choice < num_items:
item = itemgetter(item_choice)(inventory)
if item == potion:
global User_HP
User_HP += 5
lcd_print(User_HP)
break
else:
lcd_print(textscroll("ERROR! ITEM CHOICE IS AN INVALID OPTION!"))
#Defend Function def defend(User):
Enemy_damage = randint(1,3)
lcd_print(textscroll(f"The user blocked {Enemy_damage} damage"))
#Text Scrolling Function def textscroll(text):
from time import sleep
i = 0
j = 15
while True:
lcd_print(text[i:j])
i += 1
j += 1
sleep(0.2)
lcd_clear()
if j >= len(text) + 15:
break
你的错误是因为你没有给它赋值(即使你说了):
exception NameError: Raised when a local or global name is not found. This applies only to unqualified names. The associated value is an error message that includes the name that could not be found.
来源:https://docs.python.org/3/library/exceptions.html#bltin-exceptions
您的代码中的问题是您试图从一个文件访问变量而不导入它们。 全局变量只存在于它们定义的文件中。 像导入函数一样导入它们:
import Enemy_HP