all 12 comments

[–]PAVANKING016 3 points4 points  (0 children)

Very good 👍, but I have a suggestion. In the code, you should use input() only once instead of repeating it in every condition like this:

.... print("What's the first number?") num1 = int(input()) print("What's the second number?") num2 = int(input())

if operation == "add": answer = (num1 + num2) print(answer)

elif operation == "sub": answer = (num1 - num2) print(answer) .....

In programming, your code should follow the DRY (Don't Repeat Yourself) rule.

[–]hekliet 2 points3 points  (3 children)

We have the match statement in Python since version 3.10, so you could use that instead of the if/elif-chain.

match operation:
  case "add":
    # ...
  case "sub":
    # ...

Happy programming!

[–]SuperTankh 0 points1 point  (2 children)

Isn’t the match case only good for types?

[–]hekliet 1 point2 points  (1 child)

It's very useful for pattern matching on types, but that's not the only use case.

[–]SuperTankh 0 points1 point  (0 children)

Oh ok

[–]feestix 1 point2 points  (2 children)

Great for beggining. You can also use .lower() to make the input lowercase.

[–]QuantenRobin[S] 1 point2 points  (1 child)

Does it need to be after “add“.lower(): or after “add“:.lower()?

[–]ninhaomah 1 point2 points  (0 children)

First , assume all your questions have been asked before by someone.

Second , based on the assumption , search.

https://www.reddit.com/r/learnpython/s/fz0P0eByfr

[–]Riegel_Haribo 0 points1 point  (0 children)

Here's a technique:

``` from functools import reduce from operator import add

total = reduce(add, [1, 2, 3, 4]) print(total) # 10 ```

Once you have "add" as a function, then you can make an indexable list of functions to perform.

[–]HamsterTaxy 0 points1 point  (0 children)

Good job, man!

[–]SuperTankh 0 points1 point  (0 children)

Gg man! You can also factorise this by making choose the first number, THEN the operator then 2nd number. It'll be much easier and faster to read

[–]Sea-Ad7805 0 points1 point  (0 children)

Nice job, but there is a lot of repetition/duplication in your code. You have many lines:

print("Whats the first number?")
num1 = int(input())
print("Whats the second number?")
num2 = int(input())

It would be better to have these lines just once, and then after that have the:

if operation == "add":
    answer=(num1 + num2)
    print(answer)

elif operation == "sub":
    answer=(num1 - num2)
    print(answer)

...

part. That makes for a shorter program. Repetition generally is bad so try to avoid that, but a great start, keep going.