##### BEGINNER: My calculator doesnt work
 BEGINNER: My calculator doesnt work iskov Unladen Swallow Posts: 2 Threads: 1 Joined: Oct 2019 Reputation: Oct-09-2019, 06:12 AM (This post was last modified: Oct-09-2019, 06:15 AM by buran.) Hi everyone, I recently started learning how Python works and thought I would set up a simple calculator which asks for three inputs: 1. The first number 2. Operator 3. The second number This is my code: ```num1 = float(input ("Insert number: ")) operator = input ("What operation do you want to do: ") num2 = float(input ("And the second number: ")) if operator == "+" or "Plus" or "plus": print(num1 + num2) elif operator == "minus" or "Minus" or "-": print(num1 - num2) elif operator == "Multiplication" or "multiplication" or "*": print(num1 * num2) elif operator == "/" or "divide" or "Divide": print(num1 / num2) else: print("Sorry, operator not identified, please try again: ")```It does not give a Syntax Error, but whenever I insert the two numbers along with an operator it only adds up the numbers. Example: 5 * 5 = 10.0 What am I doing wrong? I really hope I am not filling up the forum with too simple stuff seeing what others are discussing in these forums. If that is the case please feel free to delete. I am just here to learn! Kind regards Reply Posts: 7,614 Threads: 133 Joined: Sep 2016 Reputation: Oct-09-2019, 06:16 AM If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein How to Ask Questions The Smart Way: link and another link Create MCV example Debug small programs Reply Posts: 1,780 Threads: 8 Joined: Jun 2018 Reputation: Oct-09-2019, 06:19 AM You never get past this comparison: `if operator == "+" or "Plus" or "plus":`It will be always True. ...and use link provided by buran. I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame. Reply iskov Unladen Swallow Posts: 2 Threads: 1 Joined: Oct 2019 Reputation: Oct-09-2019, 07:02 AM (Oct-09-2019, 06:16 AM)buran Wrote: Just read https://python-forum.io/Thread-Multiple-...or-keyword Thank you to both of you. Reply Posts: 1,780 Threads: 8 Joined: Jun 2018 Reputation: Oct-09-2019, 07:18 AM (This post was last modified: Oct-09-2019, 07:18 AM by perfringo.) We live only once so one can use it to live at the edge and write something like that: ```>>> import ast >>> first = 21 >>> op = '*' >>> second = 2 >>> exp = ast.parse(f'{first}{op}{second}', mode='eval') >>> eval(compile(exp, '', mode='eval')) 42``` I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame. Reply Posts: 7,614 Threads: 133 Joined: Sep 2016 Reputation: Oct-09-2019, 07:45 AM (This post was last modified: Oct-09-2019, 07:46 AM by buran.) @perfingo is not very explicit enough that eval() could be dangerous, so be careful when using it as an alternative to avoid using huge if/elif/else block is to use operator module ```from operator import add, mul, sub, truediv ops = {'+':add, 'plus':add, '-':sub, 'minus':sub, "multiplication":mul, "*":mul, "/":truediv, "divide":truediv} num1 = float(input ("Insert number: ")) operator = input ("What operation do you want to do: ").lower() num2 = float(input ("And the second number: ")) try: print(f'result: {ops[operator](num1, num2)}') except KeyError: print("Sorry, operator not identified, please try again")```output(multiple runs) ``````Output:Insert number: 5 What operation do you want to do: + And the second number: 4 result: 9.0````````````Output:Insert number: 5 What operation do you want to do: minus And the second number: 3 result: 2.0````````````Output:Insert number: 4 What operation do you want to do: % And the second number: 3 Sorry, operator not identified, please try again`````` If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein How to Ask Questions The Smart Way: link and another link Create MCV example Debug small programs Reply

 Possibly Related Threads… Thread Author Replies Views Last Post Why doesnt chunk generation work? LotosProgramer 0 247 Jun-18-2021, 01:13 PM Last Post: LotosProgramer if conditions in the for indentation doesnt work ? Sutsro 6 1,313 Jun-15-2021, 11:45 PM Last Post: bowlofred I have two Same Code but One of them Doesnt Work beginner721 6 779 Jan-22-2021, 10:56 PM Last Post: beginner721 code doesnt return anything ofrihemo 3 806 Jun-30-2020, 05:14 PM Last Post: ofrihemo taking input doesnt print as list bntayfur 2 800 Jun-04-2020, 02:48 AM Last Post: bntayfur Pydoc documentation doesnt work Cosmosso 4 1,635 Dec-13-2019, 08:21 AM Last Post: Cosmosso multiprocessing doesnt contain some modules(shared_memory) mike000 3 864 Oct-31-2019, 02:20 PM Last Post: mike000 Why this simple function doesnt work? blackknite 8 1,707 Jan-05-2019, 12:32 PM Last Post: buran why doesnt the while loop run? supermane 3 1,562 Aug-04-2018, 06:01 AM Last Post: wavic Why does this work and this doesnt= puruvaish24 1 1,403 May-22-2018, 03:58 AM Last Post: scidam

Forum Jump:

### User Panel Messages

##### Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020