尝试在函数中添加两个全局变量时出现语法错误



我正在尝试编写俄罗斯轮盘游戏,但我在一个特定的代码行上不断收到错误。我做了一些研究,但无法弄清楚出了什么问题。我在行的 += 部分收到无效的语法错误

global pot += global aibet

不知道为什么会这样?以下是我的其余代码用于上下文:

import random
pot = 0
playerbudget = 500
aibudget = 500
playerbet = 0
aibet = 0
deathspin = 6

def AIbet():
print()
aibet = random.randint(50,250)
print("The AI bets: $",aibet,sep="")
def AIspin():
print()
print("The AI spins the chamber...")
print("...And pulls the trigger.")
print()
dead = 6
if dead == deathspin:
print("The AI lands on the bad cartridge")
print()
global pot += global aibet
aibudget -= aibet
print("The pot is currently: $",pot,sep="")
print("The AI has a hand of $",aibudget,".",sep="")

当你说global pot += global aibet时,你对全局的使用是不正确的

在较低的范围(如函数或类(中使用global关键字告诉 python 使用该变量的全局版本而不是本地版本。(您可以使用globals()访问全局变量的字典(。

这意味着在你修改的每个函数中potaibet,你需要在使用变量之前将变量声明为global

这可能会变得混乱,除非绝对必要,否则使用这样的global变量是不受欢迎的(对于您的情况来说,这是没有必要的(。

因此,首先,为了解决您的问题,您需要:

  1. 在使用任一变量之前,将其添加到AIbet函数中。请记住,这是告诉解释器使用这些变量的全局版本而不是本地版本。
global pot
global aibet
  1. 将其添加到您的AIspin函数中。稍后在代码的全局范围的if语句中使用dead,因此还需要deadglobal
global dead
  1. 我推测您的if dead == deathspin:语句应该缩进并在AIspin函数内。如果是这种情况,您唯一需要做的更改是为potaibetaibudget添加全局声明,就像您为dead所做的那样,并将global pot += global aibet替换为pot += aibet。这样做可以为您的AIspin功能提供以下内容:
def AIspin():
global dead
global pot
global aibet
global aibudget
print()
print("The AI spins the chamber...")
print("...And pulls the trigger.")
print()
dead = 6
if dead == deathspin:
print("The AI lands on the bad cartridge")
print()
pot += aibet
aibudget -= aibet
print("The pot is currently: $",pot,sep="")
print("The AI has a hand of $",aibudget,".",sep="")

但是你不觉得这看起来很糟糕,并且充斥着不必要的global陈述吗?相反,采用基于class的方法,无需传递这么多全局变量。它看起来像这样:

import random
class RussianRoulette:
def __init__(self):
self.pot = 0
self.playerbudget = 500
self.aibudget = 500
self.playerbet = 0
self.aibet = 10
self.deathspin = 6
self.dead = 6
def ai_bet(self):
print()
self.aibet = random.randint(50,250)
print(f"The AI bets: ${self.aibet:.2f}")
return
def ai_spin(self):
print()
print("The AI spins the chamber...")
print("...And pulls the trigger.")
print()
if self.dead == self.deathspin:
print("The AI lands on the bad cartridge")
print()
self.pot += self.aibet
self.aibudget -= self.aibet
print(f"The pot is currently: ${self.pot:.2f}")
print(f"The AI has a hand of ${self.aibudget:.2f}")
return

然后,您将像这样使用此类:

game = RussianRoulette()
game.ai_spin()
game.ai_spin()
game.ai_spin()

哪些输出:

The AI spins the chamber...
...And pulls the trigger.
The AI lands on the bad cartridge
The pot is currently: $10.00
The AI has a hand of $490.00
The AI spins the chamber...
...And pulls the trigger.
The AI lands on the bad cartridge
The pot is currently: $20.00
The AI has a hand of $480.00
The AI spins the chamber...
...And pulls the trigger.
The AI lands on the bad cartridge
The pot is currently: $30.00
The AI has a hand of $470.00

摘要:避免使用global,如果可能,请使用类方法。您可以将"共享"变量打包到类实例中。类实例的作用类似于变量的容器(在您的情况下(。我们可以通过在变量名前面加上self来访问类方法中的任何位置的变量(假设这就是你对第一个参数的命名(。

考虑阅读:

  1. 易于遵循的python类教程
  2. 关于 python 类的官方文档

这不是全局的工作方式。试试这个:

global pot 
global aibet
pot += aibet

最新更新