【问题标题】:Where Do I place My Assignment?我在哪里放置我的作业?
【发布时间】:2023-03-22 04:35:01
【问题描述】:

我创建了一个程序,您可以在其中根据帐户密码“破解”虚假银行帐户以获取资金。我将money 指定为 0(因为游戏是每次打开时都会重新启动的游戏类型),将lives 指定为 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()

当它说在分配之前引用了lives。我知道,由于lives 没有在具有while lives &lt;= 100: 的代码块中分配,但是我可以在哪里放置分配以在引用它之前进行分配。我知道你可以把它放在哪里,但是......如果有人猜对了密码,我不想在进入下一个帐户时将钱重置回 0 并且将生命重置回 100。请帮忙,谢谢!!!

【问题讨论】:

  • 作为def game(): 之后的第一行添加以下内容:global live - 如果您希望能够引用它,您应该将其声明为全局!旁注:最好避免使用全局变量。仔细考虑是否真的需要 - 或者是否可以将其传递给函数!
  • @alfasin 不会让 live 变量在所有范围内都可用吗?
  • @PatrickBassut 它已经是(通过在函数之外声明它)
  • @alfasin 没看到。我以为它根本没有声明
  • @alfasin 非常感谢!!!成功了!

标签: python variables integer variable-assignment currency


【解决方案1】:

Python 的函数是function scoped。因此,您需要在函数定义中但在 while 循环之前放置生命,然后再引用它。另一方面,您可以显式声明它以引用程序中声明的全局版本的生命。或者,您可以将游戏定义为具有传递然后分配的参数,称为“生命”。

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

    • 一开始我认为它会起作用,我希望它会起作用,但是这个解决方案的输出(或者我认为的解决方案)是Traceback (most recent call last): File "C:/Users/Antonio/Desktop/moneygame.py", line 68, in &lt;module&gt; g.run() File "C:/Users/Antonio/Desktop/moneygame.py", line 47, in run if guess == passwords: UnboundLocalError: local variable 'guess' referenced before assignment
    • 这似乎是您的猜测变量的问题。确保变量中没有拼写错误,并注意self.&lt;variable&gt;(游戏对象的属性)和简单的&lt;variable&gt;(本地范围内的变量)之间的区别。
    猜你喜欢
    • 1970-01-01
    • 2019-03-27
    • 1970-01-01
    • 2011-07-16
    • 2012-04-04
    • 1970-01-01
    • 2021-02-20
    • 2020-04-01
    • 1970-01-01
    相关资源
    最近更新 更多