[英]why am i getting an UnboundLocalError after i add my while loop and if then else if statements?
Whats going on here? 这里发生了什么? It's a basic slot machine game but when i try and add my conditions for the coins and coin generating I get the UnboundLocalError
. 这是一个基本的老虎机游戏,但是当我尝试为硬币和硬币生成添加条件时,我得到UnboundLocalError
。 The random numbers generate when i don't have my while loop and if then else if statements 当我没有while循环以及if语句时生成随机数
import random
def main():
coins = 50
x = 0
y = 0
z = 0
while coins >= 0 or giveUp != yes:
coins = (coins - 3)
x = random.randrange(1, 7)
y = random.randrange(1, 7)
z = random.randrange(1, 7)
x = x
y = y
z = z
print (x + y + Z)
if (x == y) or (x == z):
if (x == 1):
coins = (coins + 3)
print ("you win 3 coins")
elif x == 2:
coins = (coins +3)
print = ("you win 3 coins")
elif x == 3:
coins = (coins + 3)
print = ("you win 3 coins")
elif x == 4:
coins = (coins +3)
print = ("you win 3 coins")
elif x == 5:
coins = (coins + 3)
print = ("you win 3 coins")
elif x == 6:
coins = (coins + 3)
print = ("you win 3 coins")
elif x == 7:
coins = (coins + 3)
print = ("you win 3 coins")
giveUp = (input("do you give up?"))
main()
This assignment (also called "binding") 此分配(也称为“绑定”)
giveUp = (input("do you give up?"))
means giveUp
is a local. 表示giveUp
是本地的。
The while
loop is trying to test this local variable before you have assigned to it (ie bound it) while
循环尝试在分配给该局部变量(即绑定它)之前测试该局部变量
You could fix this by setting giveUp = "No"
before the while
loop 您可以通过在while
循环之前设置giveUp = "No"
来解决此问题
Also the yes
should probably be "yes"
另外, yes
可能应该是"yes"
while coins >= 0 or giveUp != yes:
In here your loop condition is up to a variable called giveUp
, but you're trying to define that variable in the loop. 在这里,您的循环条件取决于一个名为giveUp
的变量,但是您试图在循环中定义该变量。 You have to define it before the loop first. 您必须在循环之前定义它。 The best solution is define giveUp="No"
before the while
loop. 最好的解决方案是在while
循环之前定义giveUp="No"
。 Also input()
returns string so it must be; 同样, input()
返回字符串,因此必须为;
giveUp="No"
while coins >= 0 or giveUp != "yes":
#codes
Another tip, you're using or
operator, so if user type no
even without any coins user still can play. 另一个提示是,您正在使用or
运算符,因此,即使用户键入no
甚至没有任何硬币,用户仍然可以玩。 You should change it to and
; 您应该将其更改为and
;
while coins >= 0 and giveUp != "yes":
Now if user want to play, need coins and 'yes' 现在,如果用户想玩,需要硬币和“是”
Please correct your code... print (x + y + Z)
name Z
in your code is Upper. 请更正您的代码...在代码中print (x + y + Z)
名称Z
为上。 Sure error in here.. and print =
name print
should not follow the character =
这里肯定有错误.. and print =
名称print
不应跟在字符=
Try this.. 尝试这个..
>>> import random
>>> def main():
coins = 50
x = 0
y = 0
z = 0
while coins >= 0 or giveUp != yes:
coins = (coins - 3)
x = random.randrange(1, 7)
y = random.randrange(1, 7)
z = random.randrange(1, 7)
x = x
y = y
z = z
print (x + y + z)
if (x == y) or (x == z):
if (x == 1):
coins = (coins + 3)
print ("you win 3 coins")
elif x == 2:
coins = (coins +3)
print ("you win 3 coins")
elif x == 3:
coins = (coins + 3)
print ("you win 3 coins")
elif x == 4:
coins = (coins +3)
print ("you win 3 coins")
elif x == 5:
coins = (coins + 3)
print ("you win 3 coins")
elif x == 6:
coins = (coins + 3)
print ("you win 3 coins")
elif x == 7:
coins = (coins + 3)
print ("you win 3 coins")
giveUp = (input("do you give up?"))
>>> main()
you win 3 coins
do you give up?3
12
12
9
7
12
9
14
10
15
you win 3 coins
do you give up?1
9
>>>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.