我正在尝试制作基于文本的rpg风格的游戏。我遇到的问题是它出现了:
UnboundLocalError:分配前引用了本地变量'print'
我不知道为什么。我在网上搜索过,发现通常是局部使用全局变量,但是我不明白为什么用print
来完成。而且,它会一直循环回到开始,而不会读出print
的{{1}}。我正在使用Python 3.7.3:
def option_statue
错误:
import time
import random
answer_A = ["A", "a"]
answer_B = ["B", "b"]
answer_C = ["C", "c"]
yes = ["Y", "y", "yes"]
no = ["N", "n", "no"]
required = ("\nUse only A, B, or C\n")
hp = 30
sword = 0
flower = 0
goblin_damage = random.randint(1,5)
def intro():
print("You wake with a start, cold mountain air fills your lungs as a bird cry shatters the serenity of the early morning.")
print("You rise to your feet and observe your surroundings,it is clear that you are in a forest of some sort, the tall oaken arms of nearby trees strech into the sky")
print(",they cast a soft shadow on you, and combined with the damp grass beneath your feet, results in a brisk morning awakening")
time.sleep(1)
print("How or why you are here remains unknown")
time.sleep(1)
print("What will you do now?")
time.sleep(1)
print("""A. Look around
B. Sit back down""")
choice = input (">>> ")
if choice in answer_A:
option_lookaround()
elif choice in answer_B:
print("\nYou sit back down.\nWell that was fun")
time.sleep(3)
print("After lying down for a while, chewing grass and contemplating why you are here, you eventually decide to do something")
time.sleep(1)
option_lookaround()
else:print (required)
intro()
def option_lookaround():
print("As you blink the sleep from your eyes, you realize that you are in a grove, in the centre, lies the broken remains of a statue")
print("to your left you see a small stream and to your right appears to be the glow of a campfire. Will you:")
print("""A. Explore the statue
B. Walk towards the stream
C. Investigate the campfire""")
choice = input (">>> ")
if choice in answer_A:
option_statue()
elif choice in answer_B:
option_stream()
elif choice in answer_C:
option_campfire()
else:
print (required)
option_lookaround()
def option_statue():
global hp
print:("""The statue appears before you, a grand monument to its era, now faded into obscurity,
it appeared to have been a humanoid of some sort, yet the eons of weather have worn the features to a near unrecognizable state""")
hp = (hp- goblin_damage)
print("You now have" ,hp, "health points")
intro()
答案 0 :(得分:0)
您的问题出在这一行:
print:("""The statue appears before you, ...""")
# ^
# Specifically here.
请看下面的文字记录,它以较小的代码段显示了您的问题:
>>> def fn():
... print:("Hello there.")
... print(42)
...
>>> fn()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 3, in fn
UnboundLocalError: local variable 'print' referenced before assignment
print:("Hello")
实际上是Python类型提示(注释)的一种形式,它看起来就像这样,只是将print
标记为局部变量(不分配任何内容给),这就是为什么您看到“分配前被引用”错误的原因。
解决方案是简单地摆脱结肠:
print("""The statue appears before you, ...""")
作为有趣的信息(嗯,对于“有趣”的一些“奇怪的”定义),关于print
的确没有什么特别的,您可以做各种事情通过分配给美好的事物:
>>> print(7)
7
>>> print = 42
>>> print(7)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'int' object is not callable
或者强迫它做超出预期的事情,例如尽管提供了42
(请不要在实际代码中做类似的事情):
def newprint(*args):
global oldprint, print
try:
oldprint
except:
oldprint = print
print = newprint
oldprint(42)
print(7) # 7
newprint(7) # 42
print(7) # 42
print("Hello", "from", "pax") # 42