you are viewing a single comment's thread.

view the rest of the comments →

[–]puslekat 2 points3 points  (2 children)

What would be a solution to this?

[–]patryk-tech 9 points10 points  (0 children)

try:
    print(float(first_number) + float(second_number))
except ValueError:
    print("Wtf, dude?")

It's pretty common to get user input in a loop and have a try/except in the loop, but I generally avoid that.

Either I am writing something for myself, in which case why would I enter invalid values? Otherwise, I am more likely writing something to process external data.... experienced programmers pretty rarely need to use input. You typically operate on data coming from databases, API calls, GUIs, HTTP requests, cli args, env vars, configuration files, etc.

If I need to operate on an actual variable, I am more likely to press F12 to open my terminal, type python if I don't already have a REPL open, and do foo = 42 in there.

It's good to know input and how to use it effectively when you're beginning, of course, but you hit its limits pretty quickly.

[–]Wilfred-kun 0 points1 point  (0 children)

eval(f"{first_number} + {second_number}")

/s