#๐ Console Math Game
27 messages ยท Page 1 of 1 (latest)
@tribal flicker
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
not bad for a begginer at all
nice job
you would be able to get rid of the GameType.ADDITION.value: ... and write GameType.ADDITION if you gave as an argument GameType.ADDITION instead of "a"
I could point out things like too much use of globals, or using print(colored_text(...)) instead of making a function print_colored_text or something like that, but, in all fairness, it's one of the best projects I've ever seen a begginer make, good job
if you want to keep up with it, you can make a recursive operation generation, so that there's more than 1 operation involved
think of something like
VALID_RANGE = (0, 100)
def generate(max_depth: int, solution: int | None = None):
if not max_depth:
s=randint(*VALID_RANGE)
return (s,),s
symbol = random.choice(['+','-','/','*'] if max_depth - 1 else ['+','-'])
match symbol:
case '+':
if solution != None:
operation, left_sol = generate(max_depth - 1)
right = solution - left_sol
return ('+', operation, right), solution
right, rsol = generate(max_depth - 1)
left, lsol = generate(max_depth - 1)
return ('+', left, right), rsol + lsol
case '-':
if solution != None:
operation, left_sol = generate(max_depth - 1)
right = left_sol - solution
return ('-', operation, right), solution
right, rsol = generate(max_depth - 1)
left, lsol = generate(max_depth - 1)
return ('-', left, right), lsol - rsol
case '*':
if solution != None:
operation, left_sol = generate(max_depth - 1)
right = solution / left_sol
return ('*', operation, right), solution
right, rsol = generate(max_depth - 1)
left, lsol = generate(max_depth - 1)
return ('*', left, right), rsol * lsol
case '/':
if solution != None:
operation, left_sol = generate(max_depth - 1)
right = left_sol/solution
return ('/', operation, right), solution
left, lsol = generate(1)
right, rsol = generate(1)
return ('/', left, right), lsol/rsol
it's just boilerplate, but if you stare at it for a while, you'll understand how's it done
it's how deep it can go
no
it's how deep,
if max_depth is 0, it can't continue, so it stops
if it's 1, it can generate 1 operation
if it's 2, then it'll make one operation in which both sides are an operation on themselves
and so on
I made it stop at division because no human will divide two floats
ใ
quite similarly though
I'll give you some examples about how that works
in the probable case you want to change it
wait there's a bug, I'll edit it real quick
generate(0) => (32,), 32
generate(1) => ('+', 50, 20), 70
generate(2) => (('*', ('-', (85,), (48,)), ('+', (72,), (56,))), -4736)
(48 - 85) * (72 + 56)
generate(3) => (('/', ('-', (50,), (39,)), ('-', (25,), (16,))), 1.2222222222222223)
(39 - 50)/(16 - 25)
...
you need another recursive function to represent it
a function like
def represent(body):
if len(body) == 1: # just a number
return str(body[0])
else:
operation, left_side, right_side = body
return f'{represent(left_side)} {operation} {represent(right_side)}'
you need a little bit more logic to preserve the order of operations
for your README.md, zoom in on the console outputs so they are easier to read, no reason to have a big blank space there ("Some Screenshots" section)
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.