我已经看过其他关于此的帖子,但我是初学者,并且不了解我见过的其他大部分内容。如果有人可以解释做什么以及如何工作,将不胜感激!这是代码。
import time
import sys
import random
done = False
day = 1
v = 2
v2 = 2
gas = 2
gas2 = 2
# thrist
dt = 0
dt2 = 0
# hunger
dh = 0
dh2 = 0
#sanity
ds = 100
ds2 = 100
#water
water = 5
water2 = 5
#food
food = 5
food2 = 5
def cls():
for i in range(10):
print('\n' * 100)
title = input('Press ENTER to start.')
cls()
playercount = input('How many players are playing?')
def player1():
start = input('''
Objective: Survive the nuke drop for 30 days.
Items:
- Food
+ 5
- Water
+ 5
- Gas Mask
- Medkit
- Gun
+ 1 ammo
- Axe
- Board Games
Survivors:
- You
''')
cls()
time.sleep(2)
while not done:
if dt > 5 and dt < 10:
input('You are getting thirsty.')
if dh > 5 and dh < 10:
input('You are getting hungry.')
if ds > 50 and ds < 70:
input('You are starting to go insane!')
if dt > 15:
input('You died of thirst')
sys.exit()
if dh > 15:
input('You died of hunger!')
sys.exit()
if ds < 50:
input('You became insane!')
sys.exit()
这是错误
Traceback (most recent call last):
File "main.py", line 892, in <module>
player1()
File "main.py", line 261, in player1
if dt > 5 and dt < 10:
UnboundLocalError: local variable 'dt' referenced before assignment
在我的代码中,这会发生多次提示帮助!
答案 0 :(得分:0)
调用函数时,如果该函数有一个未通过参数导入或未在其中声明的变量;然后发生这样的错误。
当您调用函数player1()
时,python无法找到变量dt
,因为您已将其声明在您尝试使用它的范围之外。使用参数在函数内部dt
或在player1()
函数内声明它。
对ds
和dh
变量也一样。