#๐Ÿ”’ how to convert string equation to answer?

74 messages ยท Page 1 of 1 (latest)

pale loom
#

i'm using tkinter

wise whaleBOT
#

@pale loom

Python help channel opened

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.

pale loom
#

so i want to convert addition equation like 1+2+3+4 to 10

slender flint
#

Okay

smoky dock
#

!paste can you send your code?

wise whaleBOT
#
Pasting large amounts of code

If your code is too long to fit in a codeblock in Discord, you can paste your code here:
https://paste.pythondiscord.com/

After pasting your code, save it by clicking the Paste! button in the bottom left, or by pressing CTRL + S. After doing that, you will be navigated to the new paste's page. Copy the URL and post it here so others can see it.

ember nebula
#

!e only use this is youre sure your input cant be anything malicious

input_string = '1+2+3+4'
print(eval(input_string))
wise whaleBOT
#

@ember nebula :white_check_mark: Your 3.12 eval job has completed with return code 0.

10
slender flint
#

answer = sum(int())

#

?

tender hound
ember nebula
#

so you're fine

tender hound
pale loom
tender hound
pale loom
tender hound
#

Ah well

pale loom
#

it's a tkinter entry widget

#

altho

#

i make it not type

#

whatever

#

and lock it instead

#

i think

#

btw thank y'all for helping me

median scarab
#

well, if it's a desktop app, that runs on the users computer.. the only place the user can harm is their own computer then... It only really becomes troublesome if you accept user input from the internet or something like that and run other users code on your computer

ember nebula
muted nymph
pale loom
smoky dock
#

That's what the pastebin is for. It's for larger code.

pale loom
#

there!

pale loom
#
n = input("equation: ").split("+")
print(sum(n))
```i was testign the sum function in a different file
#

eval is dangourous tho

jolly plaza
#

There is something called ast_eval I think which was much safer than eval

#

!d ast.literal_eval

wise whaleBOT
#

ast.literal_eval(node_or_string)```
Evaluate an expression node or a string containing only a Python literal or container display. The string or node provided may only consist of the following Python literal structures: strings, bytes, numbers, tuples, lists, dicts, sets, booleans, `None` and `Ellipsis`.

This can be used for evaluating strings containing Python values without the need to parse the values oneself. It is not capable of evaluating arbitrarily complex expressions, for example involving operators or indexing.
pale loom
#

uh

#

i don't wanna use them

#

sum is fine

pale loom
jolly plaza
#

!d ast

wise whaleBOT
#
ast

Source code: Lib/ast.py

The ast module helps Python applications to process trees of the Python abstract syntax grammar. The abstract syntax itself might change with each Python release; this module helps to find out programmatically what the current grammar looks like.

An abstract syntax tree can be generated by passing ast.PyCF_ONLY_AST as a flag to the compile() built-in function, or using the parse() helper provided in this module. The result will be a tree of objects whose classes all inherit from ast.AST. An abstract syntax tree can be compiled into a Python code object using the built-in compile() function.

jolly plaza
#

ast is a library

#

you gotta import/install it

pale loom
fleet nebula
#

you can use eval function?

pale loom
karmic magnet
ember nebula
#

smart

smoky dock
#

This tutorial covers everything you need to know about user interfaces in Python. I will go over all of tkinter, Python's default GUI framework, cover all the basics and then create a BMI app, a calculator and a photoshop style image editor.

If you enjoy this course, consider buying the tutorial that covers 7 additional apps: A responsive weat...

โ–ถ Play video
#

This part of the video goes over basic calculator logic with tkinter.

pale loom
#

and cs50

pale loom
smoky dock
#

I edited it, I had the wrong timestamp.

pale loom
#

i just need to turn equations into answer

pale loom
#

thanks

#

@smoky dock did you run my code?

#

the timestamp you sent is uhm

#

i already implemented the join thing

smoky dock
#

There's more to it, you have to keep watching.

pale loom
pale loom
#

@smoky dock @karmic magnet thank you all for helping but i used the regex method

#

murdox, i appreciate your help too

#

it works like a charm

wise whaleBOT
#
Python help channel closed

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.