r/PythonLearning • u/gyrojoestar31 • Sep 17 '26
Help Request any clue how to do this?
im on my first year of learning python in school is this even possible??
25
Upvotes
r/PythonLearning • u/gyrojoestar31 • Sep 17 '26
im on my first year of learning python in school is this even possible??
View all comments
3
u/FoolsSeldom Sep 17 '26
The obvious alternative to
if/elseismatch/case, but the intent is probably for you to think of more creative options than a direct substitute.Consider using functions and a dictionary approach:
add,sub,mul,divcreate a dictionary mapping each operator symbol to the corresponding function name (just the name, not the call)
result = ops[op](num1, num2)whereopsis the dictionary,opis the operation requested by the user (the operator character), andnum1andnum2are the numeric submissions from the userwhileloop to prompt the user for an operator, the loop can test if the enteredinputisinthe list of keys in the dictionary (you need to set theinputvariable to something invalid before the loop so the first test fails)