Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Python I'm trying to add a square root feature to a calculator I made, but I keep getting errors.

MacaroniChez

New Coder
In python, I made a very decent calculator that has addition, subtraction, multiplication, and division. Today, I decided that I'm gonna expand on it and add square roots. The first thing I did was Google for how to use square roots on python. It told me that I have to import in math. After that, I was able to use sqrt to add square roots. The first problem I faced with making this was that I had two variables called number1 and number2. I needed to find a way to get rid of number2, you I used code like this:

[CODE lang="python" title="Square root"]import math

operation = input("Which operation do you want to use?")
number1 = int(input("Choose a number."))
number2 = int(input("Choose another number."))

if operation == "square root":
del number1
math.sqrt(number1)

if operation == "+":
print(number1 + number2)

if operation == "-":
print(number1 - number2)

if operation == "*":
print(number1 * number2)

if operation == "/"
print(number1 // number2)][/CODE]

I didn't get an error doing this, but it square root function did not work at all. I tried doing other things, and they all resulted in error. If you know in any way how you can add square roots to this calculator, can you please let me know? Thanks!
 
Last edited by a moderator:
Hi there,

Welcome to Code Forum! :)

I know it's not efficient, however, I'm not too familiar with Python at the moment. I would try asking for the user input for the number while within the If statement so then you can ask for either both number1, number2 or just number1. And then add @TiffanyWart suggestion to have it printed to the console.
 
You could also square numbers with the exponent operator (**):
Python:
print(number1**2)
#Tells it to get the square root of 'number1'.
 
Last edited:

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom