我的点击器游戏中出现UnbloundLocalError,我不知道为什么

Nanmuhong Ye

在声明变量增量之前,我遇到了错误。因此,当我打开商店并每次点击购买更多$$$时

请帮忙。

这是我的代码:

from time import sleep as wait
import os

coin = 0
autoclick = 0
incrementation = 25
uppergrades_bought = 1
increment = 1

clear = lambda: os.system('clear') 
def store():
  cmd = int(input("What do you want to do? 1 to buy autoclick and 2 to buy more money per click").strip())
  if cmd == 1:
    coin-incrementation*uppergrades_bought
    autoclick+=1
  elif cmd == 2:
    coin-incrementation*uppergrades_bought
    increment+=1

clear()

while 1 == 1:
  print("You have {} coins!".format(coin))
  coins = input("Press enter")
  clear()
  coin+=increment
  if coin>=incrementation*uppergrades_bought:
    storeyn = input("Hi, would you like to go into the store? you have enough to buy the next uppergrade")
    if storeyn.strip().lower() == "yes" or storeyn.strip().lower() == "y":
      store()
    elif storeyn.strip().lower() == "no" or storeyn.strip().lower() == "n" :
      pass
    else:
      pass
  else:
    pass
山姆威斯

您的store函数无权访问其尝试更新的值。有很多方法可以解决这个问题- global/nonlocal正如codewelldev所建议的那样,现在需要更改的代码量最少,但是随着程序的增长,这很可能在以后给您带来很多问题,而作为IMO的新手,您应该完全避免。我的建议是重组程序,以便将所有这些信息存储在一个类中:

from time import sleep as wait
import os


class ClickerGame:
    def __init__(self):
        self.coin = 0
        self.autoclick = 0
        self.incrementation = 25
        self.upgrades_bought = 1
        self.increment = 1

    def click(self) -> None:
        """Click once, gaining coins"""
        self.coin += self.increment

    def can_afford_upgrade(self) -> bool:
        """Check if there are enough coins to afford an upgrade"""
        return self.coin >= self.incrementation * self.upgrades_bought

    def buy(self, upgrade: int) -> None:
        """1 to buy autoclick and 2 to buy more money per click"""
        if upgrade == 1:
            self.autoclick += 1
        elif upgrade == 2:
            self.increment += 1
        else:
            raise ValueError(f"Invalid upgrade option {upgrade}")            
        self.coin -= self.incrementation * self.upgrades_bought
        self.upgrades_bought += 1

    def store(self) -> None:
        """Visit the store, prompting the user for a purchase."""
        try:
            cmd = int(input(
                f"What do you want to do? {self.buy.__doc__}"
            ).strip())
            self.buy(cmd)
        except ValueError as e:
            print(e)


def clear() -> None:
    os.system('clear')


clicker = ClickerGame()


while True:
    print(f"You have {clicker.coin} coins!")
    input("Press enter")
    clear()
    clicker.click()
    if clicker.can_afford_upgrade():
        storeyn = input(
            "Hi, would you like to go into the store? "
            "you have enough to buy the next upgrade"
        ).strip().lower()
        if storeyn in {"yes", "y"}:
            clicker.store()
        elif storeyn in {"no", "n"}:
            pass
        else:
            pass
    else:
        pass

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

我不知道为什么在此python函数中出现索引错误?

来自分类Dev

我不知道为什么我得到AttributeError

来自分类Dev

我的陈述无效,我也不知道为什么

来自分类Dev

我不知道为什么我得到AttributeError

来自分类Dev

VLOOKUP给#N / A我不知道为什么

来自分类Dev

无限循环我不知道为什么(DichotomySearch)

来自分类Dev

PyGame不断崩溃,我不知道为什么

来自分类Dev

函数已执行,我不知道为什么

来自分类Dev

C ++ Segfault,我不知道为什么?

来自分类Dev

VLOOKUP给#N / A我不知道为什么

来自分类Dev

声音消失了,我不知道为什么

来自分类Dev

df挂起,我不知道为什么

来自分类Dev

不知道为什么我的python输出循环

来自分类Dev

标题缩进,我不知道为什么

来自分类Dev

不知道为什么我收到 StopIteration 错误

来自分类Dev

SMTP 错误,我不知道为什么

来自分类Dev

不知道为什么我的 cursorForObjectInConnection 返回 null?

来自分类Dev

算法停止,我不知道为什么

来自分类Dev

我不知道为什么在 MySql 中完全连接时出现此错误

来自分类Dev

我的 IF 语句出现语法错误,不知道为什么?

来自分类Dev

苹果Mach-O链接器错误,我不知道为什么

来自分类Dev

扫描器正在创建NoSuchElementException错误,我不知道为什么

来自分类Dev

我的脚本出现语法错误,但我不知道为什么以及如何解决它>

来自分类Dev

得到NullPointerException,不知道为什么。战争游戏

来自分类Dev

我创建了一个压缩器,但是存档已损坏,我也不知道为什么

来自分类Dev

我不知道为什么我的适配器未更新我的recyclerview

来自分类Dev

prog.pl第24行出现语法错误,但我不知道为什么

来自分类Dev

lynx安装后出现错误,提示“ HTTP / 1.0 302已移动”,我不知道为什么

来自分类Dev

我不知道为什么我的代码是错误的?那是什么错呢?

Related 相关文章

  1. 1

    我不知道为什么在此python函数中出现索引错误?

  2. 2

    我不知道为什么我得到AttributeError

  3. 3

    我的陈述无效,我也不知道为什么

  4. 4

    我不知道为什么我得到AttributeError

  5. 5

    VLOOKUP给#N / A我不知道为什么

  6. 6

    无限循环我不知道为什么(DichotomySearch)

  7. 7

    PyGame不断崩溃,我不知道为什么

  8. 8

    函数已执行,我不知道为什么

  9. 9

    C ++ Segfault,我不知道为什么?

  10. 10

    VLOOKUP给#N / A我不知道为什么

  11. 11

    声音消失了,我不知道为什么

  12. 12

    df挂起,我不知道为什么

  13. 13

    不知道为什么我的python输出循环

  14. 14

    标题缩进,我不知道为什么

  15. 15

    不知道为什么我收到 StopIteration 错误

  16. 16

    SMTP 错误,我不知道为什么

  17. 17

    不知道为什么我的 cursorForObjectInConnection 返回 null?

  18. 18

    算法停止,我不知道为什么

  19. 19

    我不知道为什么在 MySql 中完全连接时出现此错误

  20. 20

    我的 IF 语句出现语法错误,不知道为什么?

  21. 21

    苹果Mach-O链接器错误,我不知道为什么

  22. 22

    扫描器正在创建NoSuchElementException错误,我不知道为什么

  23. 23

    我的脚本出现语法错误,但我不知道为什么以及如何解决它>

  24. 24

    得到NullPointerException,不知道为什么。战争游戏

  25. 25

    我创建了一个压缩器,但是存档已损坏,我也不知道为什么

  26. 26

    我不知道为什么我的适配器未更新我的recyclerview

  27. 27

    prog.pl第24行出现语法错误,但我不知道为什么

  28. 28

    lynx安装后出现错误,提示“ HTTP / 1.0 302已移动”,我不知道为什么

  29. 29

    我不知道为什么我的代码是错误的?那是什么错呢?

热门标签

归档