Python 我把作业放在哪里?

Python 我把作业放在哪里?,python,variables,integer,variable-assignment,currency,Python,Variables,Integer,Variable Assignment,Currency,我创建了一个程序,你可以根据账户密码破解伪造的银行账户。我把钱分配为0,因为游戏是一种每次你打开它都会重新启动的游戏,而生命是100,因为你有100条生命来获得尽可能多的钱。代码如下: #PASSWORD GUESSING MONEY GAME # # # # import random import time print('Hello, please enter a name for your bank account.') bank_acc = input() print('Welcome

我创建了一个程序,你可以根据账户密码破解伪造的银行账户。我把钱分配为0,因为游戏是一种每次你打开它都会重新启动的游戏,而生命是100,因为你有100条生命来获得尽可能多的钱。代码如下:

#PASSWORD GUESSING MONEY GAME
#
#
#
#
import random
import time

print('Hello, please enter a name for your bank account.')
bank_acc = input()
print('Welcome to your bank account: ' + bank_acc)
print("$$$: 0")
print('Just press ENTER when ready.')
print('At the press of a button, you will have access to hundreds of millions of bank accounts.')
print('But do not be so quick, you will be required to hack the password of each bank account.')
print('Each and every password is a 3-digit code.')
print('You have 100 lives to do so. Each time you get the password wrong, subtracts a life')
print('After you use up all 100 lives, your bank account will be reset.')
print('Good luck') 

money = 0
lives = 100

def game():
    passwords = random.randint(100, 999)

    while lives <= 100:
        print('Take a guess')
        guess = input()
        guess = int(guess)

        if guess < passwords:
            print('Password incorrect. Number too low.')
            lives = lives - 1

        if guess > passwords:
            print('Password incorrect. Number too high')
            lives = lives -1

        if guess == passwords:
            break

    if guess == passwords:
        money = money + passwords
        print('Hacking account...')
        time.sleep(1)
        print('.')
        time.sleep(1)
        print('.')
        time.sleep(1)
        print('.')
        print('Account hacked.')
        print('...Adding money to account...')
        print('Your Account:')
        print('$$$: ' + str(money))
        print('Lives: ' + str(lives))
        print('...NEXT ACCOUNT...')
        print('')
        print('')
        time.sleep(2)
        game()
game()

当它说生命是在任务之前被引用的。我理解,因为life没有被分配到whilelifes的代码块中,所以Python的函数是。因此,在引用函数定义之前,需要将生命放在函数定义中,但要放在while循环之前。另一方面,您可以显式声明它以引用程序中声明的生命的全局版本。或者,您可以将游戏定义为具有一个参数,该参数被传递,然后被指定为lives。

您需要将全局变量移动到一个容器类中。在构造函数中使用下面的_uinit _;方法初始化它们,并创建一个外部循环,每次创建一个新游戏,而不是从内部调用游戏

class Game(object):
    def __init__(self):
        self.lives = 100
        self.money = 0

    def run(self):
        password = random.randint(100, 999)
        while self.lives < 100:
           ...
        ...
        time.sleep(2)
        # game()

while True:
    g = Game()
    g.run()

作为def游戏后的第一行:添加以下内容:global live-如果您希望能够引用它,您应该将其声明为global!旁注:避免使用globals是更好的做法。如果确实需要,或者可以将其传递给函数,请仔细考虑@alfasin难道它不会使live变量在所有作用域中都可用吗?@patrickbass但它已经通过在function@alfasin我没看到。我还以为它没有被申报呢all@alfasin非常感谢你!!!成功了!起初我认为它会起作用,我希望它能起作用,但这个解决方案的输出或我认为是解决方案的输出是回溯上一次调用:文件C:/Users/Antonio/Desktop/moneygame.py,第68行,在g.run文件C:/Users/Antonio/Desktop/moneygame.py,第47行,在运行中,如果guess==密码:UnboundLocalError:在赋值之前引用的局部变量“guess”可能与guess变量有关。确保变量中没有输入错误,并注意self和self之间的差异。属性和局部范围中的简单变量。