Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
有没有办法在函数中重用python输入?_Python_Variables_Nameerror - Fatal编程技术网

有没有办法在函数中重用python输入?

有没有办法在函数中重用python输入?,python,variables,nameerror,Python,Variables,Nameerror,我正在用Python编写一个冒险游戏,我在编写一个函数来检查宝藏地图中是否有宝藏时遇到了麻烦。具体来说,输入查询似乎不能在函数的其他部分重用 我正在用Sublime Text 3、Python 3.7.3编写代码,并通过Apple终端运行代码。我曾多次尝试通过flake8和black重新格式化代码,并将其清理干净 import random import time from time import sleep import sys import copy player_inventory =

我正在用Python编写一个冒险游戏,我在编写一个函数来检查宝藏地图中是否有宝藏时遇到了麻烦。具体来说,输入查询似乎不能在函数的其他部分重用

我正在用Sublime Text 3、Python 3.7.3编写代码,并通过Apple终端运行代码。我曾多次尝试通过flake8和black重新格式化代码,并将其清理干净

import random
import time
from time import sleep
import sys
import copy

player_inventory = []

class Sword:
    def __init__(self, name=None, weak=None, medium=None, 
strong=None, superstrong=None, description=None):
        self.name = name
        self.weak = weak
        self.medium = medium
        self.strong = strong
        self.superstrong = superstrong
        self.description = description

rusty_sword = Sword(name="rusty sword", weak=5, description="This is 
a rusty old sword that you found on the ground.")

gold_sword = Sword(name="gold sword", medium=15, description="This 
is a fine golden sword, with a crown engraved on the handle.")

diamond_sword = Sword(name="diamond sword", strong=45, 
description="This 100% pure diamond sword is of the finest quality. 
It reflects a prism of light when you turn it back and forth.")

plasma_sword = Sword(name="plasma sword", superstrong=135, 
description="This plasma sword can slay any opponent. With this, you 
are unstoppable.")

def printfast(str):
    for letter in str:
        sys.stdout.write(letter)
        sys.stdout.flush()
        time.sleep(0.04)

def findingtreasure0(p):
    global rusty_sword, gold_sword, diamond_sword, plasma_sword

    if p == "r" or p == "g":
        printfast("\nYou found treasure!\n")

    if p == "r":
        printfast(f"\n{rusty_sword.description}\n")
        p = rusty_sword  # maybe a problem here? 
        tstring = (f"Add {p.name} to inventory? Y/N:")
        treasurequery = input(f"{tstring}")

    elif p == "g":
        printfast(f"\n{gold_sword.description}\n")
        p = gold_sword
        tstring = (f"Add {p.name} to inventory? Y/N:")
        treasurequery = input(f"{tstring}")
    else:
        pass

    if treasurequery.lower() == "y":
        player_inventory.append(p)
        itemname.capitalize()
        print(f"The {itemname} was added to your inventory.")
        itemname.lower()
    elif treasurequery.lower() == "n":
        printfast(f"\nYou left the {itemname} alone and kept moving.\n\n")
    else:
        treasurequery
我得到一个错误:

Traceback (most recent call last):
  File "adventuregame.py", line 504, in <module>
    user_input1(firstinput)
  File "adventuregame.py", line 374, in user_input1
    findingtreasure0(tmat[x][y])
  File "adventuregame.py", line 255, in findingtreasure0
    if treasurequery.lower() == "y":
UnboundLocalError: local variable 'treasurequery' referenced before assignment
回溯(最近一次呼叫最后一次):
文件“adventuregame.py”,第504行,在
用户输入1(第一次输入)
用户输入1中第374行的“adventuregame.py”文件
finding0(tmat[x][y])
文件“adventuregame.py”,第255行,在Finding0中
如果查询.lower()=“y”:
UnboundLocalError:分配前引用的局部变量“PreceureQuery”

我希望PrecureQuery仍然保留其以前的定义,但它似乎没有这样做。

PrecureQuery
是一个局部变量;它的值在调用
finding0
之间不会持久存在。忽略许多更好的代码结构方法,您需要使其具有全局性,就像
rusty_-swool
等。具有讽刺意味的是,由于您从未为
rusty_-swool
等命名,因此它们不必标记为全局,由于在包含作用域时会自动递归查找非局部名称。稍微偏离主题,但为了更易于扩展和维护的代码,将多个Swarm类变量更改为“strength”可能更简洁。
treasurequery
是一个局部变量;它的值在调用
finding0
之间不会持久存在。忽略许多更好的代码结构方法,您需要使其具有全局性,就像
rusty_-swool
等。具有讽刺意味的是,由于您从未为
rusty_-swool
等命名,因此它们不必标记为全局,由于在包含作用域时会自动递归查找非本地名称。稍微偏离主题,但为了更易于扩展和维护的代码,将多个Swarm类变量更改为“strength”可能更简洁。