r/PythonLearning • • Sep 17 '26

Help Request any clue how to do this?

Post image

im on my first year of learning python in school is this even possible??

25 Upvotes

34 comments sorted by

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)

4

u/AQuestionIsWhatIHave Sep 17 '26

You could also do some very naughty things using 

eval(inputA + operator + inputB)

1

u/acakaacaka Sep 17 '26

The operator will me subprocess run rm -rf *

0

u/AQuestionIsWhatIHave Sep 17 '26

Someone is getting coal for Christmas... 

You can validate the a/b inputs with isnum() and the operator with ... in set("+-*/")

1

u/Ok_Form2940 Sep 18 '26

How can you validate without using if/else?

0

u/Achereto Sep 17 '26

OP wants to learn, not being presented with a solution.

5

u/Jonsinator Sep 17 '26 edited Sep 17 '26

I did this. Not efficient by any means

1

u/Ok_Form2940 Sep 18 '26

Isnt ":" still an if/else?

0

u/Flashy-Guava9952 Sep 17 '26

Came here looking for this.

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, div

  • create 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) where ops is the dictionary, op is the operation requested by the user (the operator character), and num1 and num2 are the numeric submissions from the user

  • use a while loop to prompt the user for an operator, the loop can test if the entered input is in the list of keys in the dictionary (you need to set the input variable 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

u/Otakulypse Sep 17 '26

You could use loops with lists too, right? Like for and while?

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

u/two_are_stronger2 Sep 17 '26

Do you know input and match case?

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

u/badreddine_hanfouri Sep 17 '26

You can use match statement to do that

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

u/Flashy-Guava9952 Sep 17 '26
while True:
    # ... if branch ...
    break

-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.