为什么我在添加while循环后得到一个UnboundLocalError,如果是,那么if if语句?

时间:2015-01-23 08:52:17

标签: python

这是怎么回事? 这是一个基本的老虎机游戏,但当我尝试添加硬币和硬币生成的条件时,我得到了UnboundLocalError。 当我没有我的while循环时产生随机数,如果是,则产生随机数

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()

3 个答案:

答案 0 :(得分:2)

这项任务(也称为"绑定")

giveUp = (input("do you give up?"))

表示giveUp是本地的。

while循环在分配给它之前尝试测试这个局部变量(即绑定它)

您可以通过在giveUp = "No"循环

之前设置while来解决此问题

yes也应该是"yes"

答案 1 :(得分:0)

while coins >= 0 or giveUp != yes:

在这里,你的循环条件最多是一个名为giveUp的变量,但是你试图在循环中定义该变量。你必须先在循环之前定义它。最好的解决方案是在giveUp="No"循环之前定义while 。另外input()返回字符串,因此它必须是;

giveUp="No"
while coins >= 0 or giveUp != "yes":
#codes

另一个提示,您正在使用or运算符,因此如果用户输入no即使没有任何硬币,用户仍然可以玩。您应该将其更改为and;

while coins >= 0 and giveUp != "yes":

现在,如果用户想玩,需要硬币和'是'

答案 2 :(得分:0)

请更正您的代码...... 代码中的print (x + y + Z)名称Z为Upper。 此处的确定错误.. print = 名称print不应该跟随字符=

试试这个..

>>> 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
>>>