Python error: UnboundLocalError: local variable 'score1' referenced before assignment
Python error: UnboundLocalError: local variable 'score1' referenced before assignment
我想做一个简单的掷骰子游戏,两个玩家掷一次骰子五轮,得分最高的人获胜。
我已经尝试在函数内和函数外将 score1 变量设置为 0,但这会导致分数每次都重置为 0。
#setting the scores as 0 before.
score1=0
score2=0
def round1():
print('Round 1, lets go')
input("Player 1 press ENTER to roll dice")
diceroll1()
input("Player 2 press ENTER to roll dice")
diceroll2()
round2()
#Round 1, calls upon dice roll functions and.
#dice roll function for player 1
def diceroll1():
import random
number = random.randint(1,6)
print("Your number is: " + str(number))
if number % 2 == 0:
number = number + 10
score1 = score1 + number
print("Your new score is: " + str(score1))
else:
number = number - 5
score1 = score1 + number
print("Your new score is: " + str(score1))
if score1 < 0:
score1 = 0
else:
score1=score1
#dice roll function for player 2
def diceroll2():
import random
number = random.randint(1,6)
print("Your number is: " + str(number))
score2
if number % 2 == 0:
number = number + 10
score2 = score2 + number
print("Your new score is: " + str(score2))
else:
number = number - 5
score2 = score2 + number
print("Your new score is: " + str(score2))
if score2 < 0:
score2 = 0
else:
score2=score2
我希望它简单地将骰子值添加到分数中,但我收到此错误:
UnboundLocalError: local variable 'score1' referenced before assignment
您应该使用 global
标识符。
关于代码的一些说明:
- 导入应该在代码的顶部。而且一个库导入一次就够了
- 您不必将变量的值重新定义为它自己的值。像这样
score2 = score2
。只是不要这样做。
- 我建议你使用 while 循环来无限地进行游戏,或者使用 for 循环进行 const 轮数。
- 检查您的代码并计算重复项。他们有很多。尽量减少数量。
我修改了您的代码并在其中留下了一些有趣的功能,这对您将来和现在都有帮助。
from random import randint
#setting the scores as 0 before.
score1=0
score2=0
#dice roll function for player 1
def diceroll1():
global score1
import random
number = randint(1,6)
print(f"Your number is: {str(number)}")
number += - 5 if number % 2 else 10
score1 += number
if score1 < 0:
score1 = 0
print(f"Your new score is: {str(score1)}")
#dice roll function for player 2
def diceroll2():
global score2
import random
number = randint(1,6)
print(f"Your number is: {str(number)}")
number += - 5 if number % 2 else 10
score2 += number
if score2 < 0:
score2 = 0
print(f"Your new score is: {str(score2)}")
def game_loop():
for _ in range(int(input("Raound number: "))):
print('Round 1, lets go')
input("Player 1 press ENTER to roll dice")
diceroll1()
input("Player 2 press ENTER to roll dice")
diceroll2()
print()
if __name__ == "__main__":
game_loop()
接下来,尝试将这两个功能合二为一。
这只是 Python 的一个非常常见的问题。以下是我在别处的回答。
global
和nonlocal
是我初学的时候很奇怪的东西
想想看:为什么我们在 Python 中需要它们?
因为我们不需要var
、let
之类的东西来声明变量。
想想Javascript
,也是动态脚本语言,和python很像,但需要var
或let
或const
才能声明变量。
声明变量最重要的是确定作用域
因此,在Python中,我们的变量具有隐式默认范围:定义它们的当前范围,如果我们想更改某些变量的范围,我们需要使用global
或 nonlocal
显式 .
=
左边的所有名字都是定义变量的意思
在执行某个范围的代码之前,Python会pre-compute所有local variables
,也就是=
左边的代码。 这就是为什么你得到 UnboundLocalError: local variable 'X' referenced before assignment
的原因:
def foo():
X = X + 10
因此,如果我们查找那些不在定义的当前范围内的名称,只需
遵循作用域链的规则:up, up, up and until built_in
.
记住:=
左边的任何名称的范围是默认的当前范围,你必须在引用之前分配它(绑定一些东西)它。
Global and local scope in python - Stack Overflow
使用global
。使用 global
标识符基本上就像调用它 public
,这意味着它可以从代码的所有其他部分访问。
global score1
我想做一个简单的掷骰子游戏,两个玩家掷一次骰子五轮,得分最高的人获胜。
我已经尝试在函数内和函数外将 score1 变量设置为 0,但这会导致分数每次都重置为 0。
#setting the scores as 0 before.
score1=0
score2=0
def round1():
print('Round 1, lets go')
input("Player 1 press ENTER to roll dice")
diceroll1()
input("Player 2 press ENTER to roll dice")
diceroll2()
round2()
#Round 1, calls upon dice roll functions and.
#dice roll function for player 1
def diceroll1():
import random
number = random.randint(1,6)
print("Your number is: " + str(number))
if number % 2 == 0:
number = number + 10
score1 = score1 + number
print("Your new score is: " + str(score1))
else:
number = number - 5
score1 = score1 + number
print("Your new score is: " + str(score1))
if score1 < 0:
score1 = 0
else:
score1=score1
#dice roll function for player 2
def diceroll2():
import random
number = random.randint(1,6)
print("Your number is: " + str(number))
score2
if number % 2 == 0:
number = number + 10
score2 = score2 + number
print("Your new score is: " + str(score2))
else:
number = number - 5
score2 = score2 + number
print("Your new score is: " + str(score2))
if score2 < 0:
score2 = 0
else:
score2=score2
我希望它简单地将骰子值添加到分数中,但我收到此错误:
UnboundLocalError: local variable 'score1' referenced before assignment
您应该使用 global
标识符。
关于代码的一些说明:
- 导入应该在代码的顶部。而且一个库导入一次就够了
- 您不必将变量的值重新定义为它自己的值。像这样
score2 = score2
。只是不要这样做。 - 我建议你使用 while 循环来无限地进行游戏,或者使用 for 循环进行 const 轮数。
- 检查您的代码并计算重复项。他们有很多。尽量减少数量。
我修改了您的代码并在其中留下了一些有趣的功能,这对您将来和现在都有帮助。
from random import randint
#setting the scores as 0 before.
score1=0
score2=0
#dice roll function for player 1
def diceroll1():
global score1
import random
number = randint(1,6)
print(f"Your number is: {str(number)}")
number += - 5 if number % 2 else 10
score1 += number
if score1 < 0:
score1 = 0
print(f"Your new score is: {str(score1)}")
#dice roll function for player 2
def diceroll2():
global score2
import random
number = randint(1,6)
print(f"Your number is: {str(number)}")
number += - 5 if number % 2 else 10
score2 += number
if score2 < 0:
score2 = 0
print(f"Your new score is: {str(score2)}")
def game_loop():
for _ in range(int(input("Raound number: "))):
print('Round 1, lets go')
input("Player 1 press ENTER to roll dice")
diceroll1()
input("Player 2 press ENTER to roll dice")
diceroll2()
print()
if __name__ == "__main__":
game_loop()
接下来,尝试将这两个功能合二为一。
这只是 Python 的一个非常常见的问题。以下是我在别处的回答。
global
和nonlocal
是我初学的时候很奇怪的东西
想想看:为什么我们在 Python 中需要它们?
因为我们不需要var
、let
之类的东西来声明变量。
想想Javascript
,也是动态脚本语言,和python很像,但需要var
或let
或const
才能声明变量。
声明变量最重要的是确定作用域
因此,在Python中,我们的变量具有隐式默认范围:定义它们的当前范围,如果我们想更改某些变量的范围,我们需要使用global
或 nonlocal
显式 .
=
左边的所有名字都是定义变量的意思
在执行某个范围的代码之前,Python会pre-compute所有local variables
,也就是=
左边的代码。 这就是为什么你得到 UnboundLocalError: local variable 'X' referenced before assignment
的原因:
def foo():
X = X + 10
因此,如果我们查找那些不在定义的当前范围内的名称,只需
遵循作用域链的规则:up, up, up and until built_in
.
记住:=
左边的任何名称的范围是默认的当前范围,你必须在引用之前分配它(绑定一些东西)它。
Global and local scope in python - Stack Overflow
使用global
。使用 global
标识符基本上就像调用它 public
,这意味着它可以从代码的所有其他部分访问。
global score1