calculator updates '=' button

This commit is contained in:
2023-06-09 19:33:41 +10:00
parent a81359a274
commit 95ff2e7e46
3 changed files with 22 additions and 1 deletions

View File

@@ -23,6 +23,7 @@ class Calculator(discord.ui.View):
self.division: "/", self.division: "/",
self.exponent: "^" self.exponent: "^"
} }
self.clearRequirement = 0
def numDigits(self, integer): def numDigits(self, integer):
numDigits = 0 numDigits = 0
@@ -31,6 +32,11 @@ class Calculator(discord.ui.View):
integer = integer // 10 integer = integer // 10
return numDigits return numDigits
def clear(self):
self.inputOne = 0
self.inputTwo = None
self.output = None
def display(self): def display(self):
inputOneLength = self.numDigits(self.inputOne) inputOneLength = self.numDigits(self.inputOne)
inputTwoLength = self.numDigits(self.inputTwo) inputTwoLength = self.numDigits(self.inputTwo)
@@ -47,6 +53,9 @@ class Calculator(discord.ui.View):
return displayOutput return displayOutput
async def number(self, interaction: discord.Interaction, number): async def number(self, interaction: discord.Interaction, number):
if self.output is not None:
self.inputOne = 0
self.output = None
self.inputOne = self.inputOne * 10 + number self.inputOne = self.inputOne * 10 + number
self.numDigs = self.numDigs + 1 self.numDigs = self.numDigs + 1
await interaction.response.edit_message( await interaction.response.edit_message(
@@ -54,6 +63,7 @@ class Calculator(discord.ui.View):
view=self) view=self)
async def operator(self, interaction: discord.Interaction, operation): async def operator(self, interaction: discord.Interaction, operation):
self.output = None
self.inputTwo = self.inputOne self.inputTwo = self.inputOne
self.inputOne = 0 self.inputOne = 0
self.expression = operation self.expression = operation
@@ -119,6 +129,10 @@ class Calculator(discord.ui.View):
@discord.ui.button(label="=", style=discord.ButtonStyle.green, row=4) @discord.ui.button(label="=", style=discord.ButtonStyle.green, row=4)
async def equals(self, interaction: discord.Interaction, button: discord.ui.Button): async def equals(self, interaction: discord.Interaction, button: discord.ui.Button):
if self.clearRequirement:
self.clear()
else:
self.clearRequirement += 1
self.output = self.expression(self.inputTwo, self.inputOne) self.output = self.expression(self.inputTwo, self.inputOne)
if (self.inputTwo == 9) and (self.inputOne == 10): if (self.inputTwo == 9) and (self.inputOne == 10):
self.output = 21 self.output = 21
@@ -126,6 +140,9 @@ class Calculator(discord.ui.View):
content=self.display(), content=self.display(),
view=self) view=self)
self.inputOne = self.output
self.inputTwo = None
@discord.ui.button(label="/", style=discord.ButtonStyle.red, row=4) @discord.ui.button(label="/", style=discord.ButtonStyle.red, row=4)
async def divide(self, interaction: discord.Interaction, button: discord.ui.Button): async def divide(self, interaction: discord.Interaction, button: discord.ui.Button):
await self.operator(interaction, self.division) await self.operator(interaction, self.division)

View File

@@ -15,13 +15,17 @@ games = ["Left 4 Dead 2",
"Magicka 2", "Magicka 2",
] ]
def add_game(game: str):
games.append(game)
def pick_game(): def pick_game():
randomIndex = random.randrange(0, len(games)) randomIndex = random.randrange(0, len(games))
coinFlip = random.randrange(0, 2) coinFlip = random.randrange(0, 2)
if coinFlip: if coinFlip:
return "Valorant" return f"Valorant ({games[randomIndex]})"
else: else:
return games[randomIndex] return games[randomIndex]

Binary file not shown.