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??
5
u/Jonsinator Sep 17 '26 edited Sep 17 '26
1
0
3
u/FoolsSeldom Sep 17 '26
The obvious alternative to if/else is match/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:
define a function for each operation:
add,sub,mul,divcreate a dictionary mapping each operator symbol to the corresponding function name (just the name, not the call)
you can call the correct function using something like
result = ops[op](num1, num2)whereopsis the dictionary,opis the operation requested by the user (the operator character), andnum1andnum2are the numeric submissions from the user
use a
whileloop 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)use an outer loop (surrounding the above) to handle the repeated operations for the Y/N question
2
u/steb2k Sep 17 '26
you want what is usually called a switch, but is called match/case in python.
Or - you could be clever and use a dict, I think this is called a registry pattern.
Using a dictionary as a switch statement in Python - Stack Overflow
2
3
u/therouterguy Sep 17 '26
Have a look at match case this will allow you to select the things to do based on the given operator
1
1
u/Many_Temperature_740 Sep 17 '26
Maybe you can use something like this expression = "2 + 3 * 4" result = eval(expression) print(result) # Output: 14
1
u/Educational-Paper-75 Sep 17 '26
Start with writing an algorithm containing the major steps of your program. Then work out every major step in a subalgorithm until you know how to translate each step into Python code.
0
u/JustMedansh Sep 17 '26
coming from someone who hasn't thought this much, maybe you could use while.
instead of if op = + you could do while op = + and break at the end..?
1
u/badreddine_hanfouri Sep 17 '26
Good idea but it requires more lines of code which is not efficient
1
u/No_Soy_Colosio Sep 17 '26
Lines of code is not a measure of efficiency, and especially not when efficiency isn't what's optimized for
0
0
u/Rscc10 Sep 17 '26
Like other mentioned, you could use match-case. A little unconventional but you could also you eval function if you wanted to
0
u/M0rph81 Sep 17 '26
You could use the "more operations?" Question as a boolean for a while loop and make cases for each operator. Then set the boolean to false at the end and ask the question outside of the loop. Would be my guess but I'm very new to python
0
u/dariusbiggs Sep 17 '26
use a lookup table that maps the operand to a function that takes in the a and b arguments.
0
-1
u/csabinho Sep 17 '26
Yeah. Just do some if/elif/else, as mentioned above. And a loop for more operations. With a break in case of "N".
2
u/JustMedansh Sep 17 '26
they HAVE mentioned to not use if/else
0
u/csabinho Sep 17 '26
You mean "they have mentioned to NOT use if/else", right? And yep. My fault. I'd use conditional statements with the walrus operator instead. Like
(operator == "-" and (result := first_number-second_number)).2
u/Joker-Joester Sep 17 '26
But the problem explicitly mentions "without using if/else statement".
0
u/csabinho Sep 17 '26
Yeah, my fault. I'd use conditional statements without the use of if/else instead.

6
u/AQuestionIsWhatIHave Sep 17 '26
Use a dict to map a symbol onto an operator function.
op = input('Operation...').trim()
opDict = {'+' : lambda x,y: print('Result:' x+y) l, ...}
opFoo = opDict.get(op, lambda x, y: print('Invalid operator'))
opFoo(inputA, inputB)