#๐ how to convert string equation to answer?
74 messages ยท Page 1 of 1 (latest)
@pale loom
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.
so i want to convert addition equation like 1+2+3+4 to 10
Okay
!paste can you send your 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.
!e only use this is youre sure your input cant be anything malicious
input_string = '1+2+3+4'
print(eval(input_string))
@ember nebula :white_check_mark: Your 3.12 eval job has completed with return code 0.
10
isn't eval dangourese?
Only if the user can do something malicious
Seeing as how this is a calculator, I don't see how the input_string could be something malicious
so you're fine
Which is about 99% of the time
i won' use it.
You are fine if you use it here
no
Ah well
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
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
well thats new
yes, it is unsafe
no it's too big lol
That's what the pastebin is for. It's for larger code.
sorry mb i didn't read it properly
n = input("equation: ").split("+")
print(sum(n))
```i was testign the sum function in a different file
eval is dangourous tho
There is something called ast_eval I think which was much safer than eval
!d ast.literal_eval
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.
no in my ide it says it's not diffined
!d 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.
figured that out as i sent the message
you can use eval function?
that's unsafe
Could also validate input: test input string against a regex (only digits and expected characters, etc).
yeahhh
i know regex
smart
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...
This part of the video goes over basic calculator logic with tkinter.
i am actually a part of clear code's server
and cs50
uhm, i think i know that stuff
I edited it, I had the wrong timestamp.
i just need to turn equations into answer
makes sense
thanks
@smoky dock did you run my code?
the timestamp you sent is uhm
i already implemented the join thing
There's more to it, you have to keep watching.
hurts my head man
imma just do this
@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
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.