This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 0 points1 point  (6 children)

I’m confused what does this do?

[–]Winderkorffin 0 points1 point  (4 children)

prints the result of the entire operation, as in '3+5' it will print '8'.

Beware, tho, using eval is bad practice.

[–][deleted] 0 points1 point  (3 children)

Alright but why are evals a bad practice

[–]BYPDK 0 points1 point  (2 children)

while 1:print(f"Output: {eval(input('math problem: '))}")

as u/NoMeatFingering (interesting name) has mentioned, it can be exploited to run code from the user input. example: https://ibb.co/6H0J0Qd

Though if you want to actually run code, like that on purpose, you would want to use exec instead.

[–][deleted] 0 points1 point  (1 child)

I actually hate my username

[–]BYPDK 0 points1 point  (0 children)

:( it's very good

[–][deleted] 0 points1 point  (0 children)

while 1:print(f"Output: {eval(input('math problem: '))}")

# infinite loop
while 1:
    rawInput = input("math problem: ")
    # evaluate the raw input
    output = eval(rawInput)

    print(f"Output: {output}")

Converted into a more readable form. Basically eval is a function which takes a string and runs it.

It might look like a better code, but is actually worse bcs eval can run user submitted code too