1

I'm trying to code a very simple calculator in python. The problem is it works perfectly when I give positive numbers but I don't get any answer when I enter negative numbers... I can't figure out why. I would really appreciate it if someone could help me :). Here's my code:

print("Hi :) This is your simple calculator") a = float(input("please enter the first number: ")) b = float(input("please enter the second number: ")) print(f"{a}+{b} is {round((a)+(b), 3)}, {a}-{b} is {round(a-(b), 3)}, {b}-{a} is {round(b-(a), 3)}, {a}*{b} is {round(a*(b), 3)}, {a}/{b} is {round(a/(b), 3 )}, {b}/{a} is {round(b/(a), 3)}, {a} to the power of {b} is {round((a)**(b), 3)}, {b}to the power of{a} is {round((b)**(a), 3)}, {b} root of {a} is {round((a)**(1/b), 3)}, {a} root of {b} is {round((b)**(1/a), 3)} ") 
1
  • 1
    What do you get when you enter negative numbers? That's important to mention when you're asking for help. Please read How to Ask and the question checklist. Welcome to Stack Overflow! Commented Apr 23, 2021 at 14:35

2 Answers 2

2

Here is the print statement revised (without the roots). This part seems to work.

print(f"{a}+{b} is {round((a)+(b), 3)}, {a}-{b} is {round(a-(b), 3)}, {b}-{a} is {round(b-(a), 3)}, {a}{b} is {round(a * b, 3)}, {a} to the power of {b} is {round(a ** b, 3)}, {b}to the power of{a} is {round(b ** a, 3)}") 

I took off the roots because you would run into problems with roots of negative numbers. For complex numbers look up the cmath module.

Sign up to request clarification or add additional context in comments.

Comments

0

This is how I would have done it. The problem is that you cannot take the nth root of negative numbers. This will lead to imaginary numbers that you will need to use c.math for. Also, you need to be careful with division of 0's. I have made some if-else conditions if a user has a to be a negative number or b to be a negative number.

import math print("Hi :) This is your simple calculator") a = float(input("please enter the first number: ")) b = float(input("please enter the second number: ")) print(str(a) + ' + ' + str(b) + ' is ' + str(round(a + b, 3))) print(str(a) + ' - ' + str(b) + ' is ' + str(round(a - b, 3))) print(str(b) + ' - ' + str(a) + ' is ' + str(round(b - a, 3))) print(str(a) + ' * ' + str(b) + ' is ' + str(round(a * b, 3))) if b == 0: print('Error! Cannot divide a by 0!') else: print(str(a) + ' / ' + str(b) + ' is ' + str(round(a / b, 3))) if a == 0: print('Error! Cannot divide b by 0!') else: print(str(b) + ' / ' + str(a) + ' is ' + str(round(b / a, 3))) if (a < 0 or b == 0): print('Please enter a positive number for a.') else: print(str(a) + ' ** ' + str(b) + ' is ' + str(round(pow(a, b), 3))) print(str(a) + ' root ' + str(b) + ' is ' + str(round(pow(a, (1/float(b))), 3))) if (b < 0 or a == 0): print('Please enter a positive number for b.') else: print(str(b) + ' ** ' + str(a) + ' is ' + str(round(pow(b, a), 3))) print(str(b) + ' root ' + str(a) + ' is ' + str(round(pow(b, (1/float(a))), 3))) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.