Calculator - Equals is broken
This commit is contained in:
274
Calculator.py
274
Calculator.py
@@ -1,155 +1,151 @@
|
|||||||
import discord
|
import discord
|
||||||
|
|
||||||
|
addition = lambda x, y: x + y
|
||||||
|
subtraction = lambda x, y: x - y
|
||||||
|
multiplication = lambda x, y: x * y
|
||||||
|
division = lambda x, y: x / y
|
||||||
|
exponent = lambda x, y: x ** y
|
||||||
|
|
||||||
|
funckyDict = {
|
||||||
|
addition: "+",
|
||||||
|
subtraction: "-",
|
||||||
|
multiplication: "*",
|
||||||
|
division: "/",
|
||||||
|
exponent: "^"
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
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.numDigs = self.numDigs + 1
|
||||||
|
await interaction.response.edit_message(
|
||||||
|
content=self.display(),
|
||||||
|
view=self)
|
||||||
|
|
||||||
|
async def operator(self, interaction: discord.Interaction, operation):
|
||||||
|
self.output = None
|
||||||
|
self.inputTwo = self.inputOne
|
||||||
|
self.inputOne = 0
|
||||||
|
self.expression = operation
|
||||||
|
await interaction.response.edit_message(
|
||||||
|
content=self.display(),
|
||||||
|
view=self)
|
||||||
|
|
||||||
|
|
||||||
|
class NumberButton(discord.ui.Button):
|
||||||
|
|
||||||
|
def __init__(self, number: int, row) -> None:
|
||||||
|
super().__init__()
|
||||||
|
self.number = number
|
||||||
|
self.row = row
|
||||||
|
self.style = discord.ButtonStyle.blurple
|
||||||
|
self.label = str(number)
|
||||||
|
|
||||||
|
async def callback(self, interaction: discord.Interaction):
|
||||||
|
await number(self.view, interaction, self.number)
|
||||||
|
|
||||||
|
class OperatorButton(discord.ui.Button):
|
||||||
|
|
||||||
|
def __init__(self, function, row) -> None:
|
||||||
|
super().__init__()
|
||||||
|
self.function = function
|
||||||
|
self.row = row
|
||||||
|
self.style = discord.ButtonStyle.red
|
||||||
|
self.label = funckyDict[function]
|
||||||
|
|
||||||
|
async def callback(self, interaction: discord.Interaction):
|
||||||
|
await operator(self.view, interaction, self.function)
|
||||||
|
|
||||||
class Calculator(discord.ui.View):
|
class Calculator(discord.ui.View):
|
||||||
def __init__(self, *, timeout=180):
|
def __init__(self, *, timeout=180):
|
||||||
super().__init__(timeout=timeout)
|
super().__init__(timeout=timeout)
|
||||||
self.numDigs = 0
|
self.numDigs = 0
|
||||||
self.MAXIMUMBARSIZE = 30
|
self.MAXIMUMBARSIZE = 30
|
||||||
m = 0
|
m = 0
|
||||||
self.inputOne = 0
|
self.inputOne = 0
|
||||||
self.inputTwo = None
|
self.inputTwo = None
|
||||||
self.expression = None
|
self.expression = None
|
||||||
self.output = None
|
self.output = None
|
||||||
self.digit = 1
|
self.digit = 1
|
||||||
self.addition = lambda x, y: x + y
|
|
||||||
self.subtraction = lambda x, y: x - y
|
|
||||||
self.multiplication = lambda x, y: x * y
|
|
||||||
self.division = lambda x, y: x / y
|
|
||||||
self.exponent = lambda x, y: x ** y
|
|
||||||
self.funckyDict = {
|
|
||||||
self.addition: "+",
|
|
||||||
self.subtraction: "-",
|
|
||||||
self.multiplication: "*",
|
|
||||||
self.division: "/",
|
|
||||||
self.exponent: "^"
|
|
||||||
}
|
|
||||||
self.clearRequirement = 0
|
|
||||||
|
|
||||||
def numDigits(self, integer):
|
|
||||||
numDigits = 0
|
|
||||||
while(integer):
|
|
||||||
numDigits += 1
|
|
||||||
integer = integer // 10
|
|
||||||
return numDigits
|
|
||||||
|
|
||||||
def clear(self):
|
self.clearRequirement = 0
|
||||||
self.inputOne = 0
|
|
||||||
self.inputTwo = None
|
|
||||||
self.output = None
|
|
||||||
self.clearRequirement = 0
|
|
||||||
|
|
||||||
def display(self):
|
numRows = 4
|
||||||
inputOneLength = self.numDigits(self.inputOne)
|
numColumns = 4
|
||||||
inputTwoLength = self.numDigits(self.inputTwo)
|
|
||||||
outputLength = self.numDigits(self.output)
|
|
||||||
if (self.inputTwo is None) and (self.output is None):
|
|
||||||
padding = self.MAXIMUMBARSIZE - inputOneLength - 1
|
|
||||||
displayOutput = " ".join(['`', ' ' * padding, f"{self.inputOne}", "`"])
|
|
||||||
elif (self.output is None):
|
|
||||||
padding = self.MAXIMUMBARSIZE - inputOneLength - inputTwoLength - 3
|
|
||||||
displayOutput = " ".join(['`', ' ' * padding, f"{self.inputTwo}", f"{self.funckyDict[self.expression]}", f"{self.inputOne}", "`"])
|
|
||||||
else:
|
|
||||||
padding = self.MAXIMUMBARSIZE - inputOneLength - inputTwoLength - outputLength - 4
|
|
||||||
displayOutput = " ".join(['`', ' ' * padding, f"{self.inputTwo}", f"{self.funckyDict[self.expression]}", f"{self.inputOne}", "=", f"{self.output}", "`"])
|
|
||||||
return displayOutput
|
|
||||||
|
|
||||||
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.numDigs = self.numDigs + 1
|
|
||||||
await interaction.response.edit_message(
|
|
||||||
content=self.display(),
|
|
||||||
view=self)
|
|
||||||
|
|
||||||
async def operator(self, interaction: discord.Interaction, operation):
|
|
||||||
self.output = None
|
|
||||||
self.inputTwo = self.inputOne
|
|
||||||
self.inputOne = 0
|
|
||||||
self.expression = operation
|
|
||||||
await interaction.response.edit_message(
|
|
||||||
content=self.display(),
|
|
||||||
view=self)
|
|
||||||
|
|
||||||
@discord.ui.button(label="^", style=discord.ButtonStyle.red, row=4)
|
buttonFunctions = [
|
||||||
async def power(self, interaction: discord.Interaction, button: discord.ui.Button):
|
[1, 2, 3, addition],
|
||||||
await self.operator(interaction, self.exponent)
|
[4, 5, 6, multiplication],
|
||||||
|
[7, 8, 9, subtraction],
|
||||||
@discord.ui.button(label="0", style=discord.ButtonStyle.blurple,row=4)
|
[exponent, 0, None, division]
|
||||||
async def zero(self, interaction: discord.Interaction, button: discord.ui.Button):
|
]
|
||||||
await self.number(interaction, 0)
|
|
||||||
|
|
||||||
@discord.ui.button(label="1", style=discord.ButtonStyle.blurple, row=1)
|
|
||||||
async def one(self, interaction: discord.Interaction, button: discord.ui.Button):
|
|
||||||
await self.number(interaction, 1)
|
|
||||||
|
|
||||||
@discord.ui.button(label="2", style=discord.ButtonStyle.blurple, row=1)
|
for i in range(0, numRows):
|
||||||
async def two(self, interaction: discord.Interaction, button: discord.ui.Button):
|
for j in range(0, numColumns):
|
||||||
await self.number(interaction, 2)
|
item = buttonFunctions[i][j]
|
||||||
|
if item is None:
|
||||||
|
continue
|
||||||
|
if isinstance(item, int):
|
||||||
|
|
||||||
@discord.ui.button(label="3", style=discord.ButtonStyle.blurple, row=1)
|
button = NumberButton(item, i)
|
||||||
async def three(self, interaction: discord.Interaction, button: discord.ui.Button):
|
else:
|
||||||
await self.number(interaction, 3)
|
|
||||||
|
|
||||||
@discord.ui.button(label="4", style=discord.ButtonStyle.blurple, row=2)
|
button = OperatorButton(item, i)
|
||||||
async def four(self, interaction: discord.Interaction, button: discord.ui.Button):
|
self.add_item(button)
|
||||||
await self.number(interaction, 4)
|
|
||||||
|
|
||||||
@discord.ui.button(label="5", style=discord.ButtonStyle.blurple, row=2)
|
|
||||||
async def five(self, interaction: discord.Interaction, button: discord.ui.Button):
|
|
||||||
await self.number(interaction, 5)
|
|
||||||
|
|
||||||
@discord.ui.button(label="6", style=discord.ButtonStyle.blurple, row=2)
|
|
||||||
async def six(self, interaction: discord.Interaction, button: discord.ui.Button):
|
|
||||||
await self.number(interaction, 6)
|
|
||||||
|
|
||||||
@discord.ui.button(label="7", style=discord.ButtonStyle.blurple, row=3)
|
def numDigits(self, integer):
|
||||||
async def seven(self, interaction: discord.Interaction, button: discord.ui.Button):
|
numDigits = 0
|
||||||
await self.number(interaction, 7)
|
while(integer):
|
||||||
|
numDigits += 1
|
||||||
|
integer = integer // 10
|
||||||
|
return numDigits
|
||||||
|
|
||||||
@discord.ui.button(label="8", style=discord.ButtonStyle.blurple, row=3)
|
def clear(self):
|
||||||
async def eight(self, interaction: discord.Interaction, button: discord.ui.Button):
|
self.inputOne = 0
|
||||||
await self.number(interaction, 8)
|
self.inputTwo = None
|
||||||
|
self.output = None
|
||||||
|
self.clearRequirement = 0
|
||||||
|
|
||||||
@discord.ui.button(label="9", style=discord.ButtonStyle.blurple, row=3)
|
def display(self):
|
||||||
async def nine(self, interaction: discord.Interaction, button: discord.ui.Button):
|
inputOneLength = self.numDigits(self.inputOne)
|
||||||
await self.number(interaction, 9)
|
inputTwoLength = self.numDigits(self.inputTwo)
|
||||||
|
outputLength = self.numDigits(self.output)
|
||||||
|
if (self.inputTwo is None) and (self.output is None):
|
||||||
|
padding = self.MAXIMUMBARSIZE - inputOneLength - 1
|
||||||
|
displayOutput = " ".join(['`', ' ' * padding, f"{self.inputOne}", "`"])
|
||||||
|
elif (self.output is None):
|
||||||
|
padding = self.MAXIMUMBARSIZE - inputOneLength - inputTwoLength - 3
|
||||||
|
displayOutput = " ".join(['`', ' ' * padding, f"{self.inputTwo}", f"{funckyDict[self.expression]}", f"{self.inputOne}", "`"])
|
||||||
|
else:
|
||||||
|
padding = self.MAXIMUMBARSIZE - inputOneLength - inputTwoLength - outputLength - 4
|
||||||
|
displayOutput = " ".join(['`', ' ' * padding, f"{self.inputTwo}", f"{funckyDict[self.expression]}", f"{self.inputOne}", "=", f"{self.output}", "`"])
|
||||||
|
return displayOutput
|
||||||
|
|
||||||
@discord.ui.button(label="+", style=discord.ButtonStyle.red, row=1)
|
@discord.ui.button(label="=", style=discord.ButtonStyle.green, row=4)
|
||||||
async def plus(self, interaction: discord.Interaction, button: discord.ui.Button):
|
async def equals(self, interaction: discord.Interaction, button: discord.ui.Button):
|
||||||
await self.operator(interaction, self.addition)
|
|
||||||
|
if self.inputTwo is None:
|
||||||
@discord.ui.button(label="*", style=discord.ButtonStyle.red, row=2)
|
await interaction.response.edit_message(
|
||||||
async def mult(self, interaction: discord.Interaction, button: discord.ui.Button):
|
content="ERROR",
|
||||||
await self.operator(interaction, self.multiplication)
|
view=self)
|
||||||
|
|
||||||
@discord.ui.button(label="-", style=discord.ButtonStyle.red, row=3)
|
if self.clearRequirement:
|
||||||
async def minus(self, interaction: discord.Interaction, button: discord.ui.Button):
|
self.clearRequirement = 0
|
||||||
await self.operator(interaction, self.subtraction)
|
self.clear()
|
||||||
|
else:
|
||||||
@discord.ui.button(label="=", style=discord.ButtonStyle.green, row=4)
|
self.clearRequirement += 1
|
||||||
async def equals(self, interaction: discord.Interaction, button: discord.ui.Button):
|
self.output = self.expression(self.inputTwo, self.inputOne)
|
||||||
|
if (self.inputTwo == 9) and (self.inputOne == 10):
|
||||||
if self.inputTwo is None:
|
self.output = 21
|
||||||
await interaction.response.edit_message(
|
await interaction.response.edit_message(
|
||||||
content="ERROR",
|
content=self.display(),
|
||||||
view=self)
|
view=self)
|
||||||
|
|
||||||
if self.clearRequirement:
|
self.inputOne = self.output
|
||||||
self.clear()
|
self.inputTwo = None
|
||||||
else:
|
|
||||||
self.clearRequirement += 1
|
|
||||||
self.output = self.expression(self.inputTwo, self.inputOne)
|
|
||||||
if (self.inputTwo == 9) and (self.inputOne == 10):
|
|
||||||
self.output = 21
|
|
||||||
await interaction.response.edit_message(
|
|
||||||
content=self.display(),
|
|
||||||
view=self)
|
|
||||||
|
|
||||||
self.inputOne = self.output
|
|
||||||
self.inputTwo = None
|
|
||||||
|
|
||||||
@discord.ui.button(label="/", style=discord.ButtonStyle.red, row=4)
|
|
||||||
async def divide(self, interaction: discord.Interaction, button: discord.ui.Button):
|
|
||||||
await self.operator(interaction, self.division)
|
|
||||||
@@ -22,6 +22,7 @@ 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)
|
||||||
|
print(coinFlip)
|
||||||
if coinFlip:
|
if coinFlip:
|
||||||
|
|
||||||
return f"Valorant ({games[randomIndex]})"
|
return f"Valorant ({games[randomIndex]})"
|
||||||
@@ -29,18 +30,3 @@ def pick_game():
|
|||||||
|
|
||||||
return games[randomIndex]
|
return games[randomIndex]
|
||||||
|
|
||||||
I = 1000000000
|
|
||||||
i = I
|
|
||||||
zeros = 0
|
|
||||||
ones = 0
|
|
||||||
while i > 0:
|
|
||||||
number = random.randrange(0, 2)
|
|
||||||
if number:
|
|
||||||
ones += 1
|
|
||||||
|
|
||||||
else:
|
|
||||||
zeros += 1
|
|
||||||
i -= 1
|
|
||||||
|
|
||||||
percentZeros = zeros/I
|
|
||||||
print(percentZeros)
|
|
||||||
|
|||||||
Reference in New Issue
Block a user