-1

I'm making a calculator program, it has a square root feature, but first, you need to type 's' in order to access it, I wanna make it so that the user can type "S" or "s" and have the computer still recognize it and bring up the square root option but if I add the s.upper() and the s variable it works but not as intended the code is:

import math

def calculator():
    while True:
        s = "s"
        intro = input('Hello! Please type * for multiplication, / for division, + for addition, - for subtraction, ** for exponents, and "s" for square root \n')
        if intro not in ["*", "/", "+", "-", "**", s.upper(), s]:
            print ("that wasnt an option!")
            continue
        if intro != s:
           num1 = int(input("Whats your first number \n"))
           num2 = int(input("Whats your second number \n"))
        if intro != s.upper():
           num1 = int(input("Whats your first number \n"))
           num2 = int(input("Whats your second number \n"))  
        if intro == "*":
            print(num1 * num2)
            break
        elif intro == "/":
            print(num1/num2)
            break
        elif intro == "+":
            print(num1 + num2)
            break
        elif intro == "-":
            print(num1 - num2)
            break
        elif intro == "**":
            print(num1 ** num2)
            break
        elif intro == s.upper():
            num_sqr = int(input("What number do you wanna find the square root of \n"))
            print(math.sqrt(num_sqr))
            break
        elif intro == s:
            num_sqr = int(input("What number do you wanna find the square root of \n"))
            print(math.sqrt(num_sqr))
            break
            
            
calculator()

whenever a user types s it ignores variables num1 and num2 so it can just do run the num_sqr variable so the output is:

s (or S)

Whats your first number 
2

Whats your second number 
4

What number do you wanna find the square root of 
24
4.898979485566356

Rather than:

s (or S)

What number do you wanna find the square root of 
24
4.898979485566356

Why is it doing this and how can I fix it?

dareesome
  • 133
  • 1
  • 7

5 Answers5

0

Go ahead and call the .capitalize() string method over your intro string; it'll insure the correct formatting for the string letters without messing up the formatting on the operators.

import math

def calculator():
    while True:
        intro = input('Hello! Please type * for multiplication, / for division, + for addition, - for subtraction, ** for exponents, and "s" for square root \n')
        intro = intro.capitalize()
        if intro not in ["*", "/", "+", "-", "**", "S"]:
            print ("that wasnt an option!")
            continue
        if intro != "S":
           num1 = int(input("Whats your first number \n"))
           num2 = int(input("Whats your second number \n"))
        if intro == "*":
            print(num1 * num2)
            break
        elif intro == "/":
            print(num1/num2)
            break
        elif intro == "+":
            print(num1 + num2)
            break
        elif intro == "-":
            print(num1 - num2)
            break
        elif intro == "**":
            print(num1 ** num2)
            break
        else:
            num_sqr = int(input("What number do you wanna find the square root of \n"))
            print(math.sqrt(num_sqr))
            break
            
            
calculator()


Also just for fun, since this is evaluating string expressions, Python actually provides us with a handy function for applying computations over string objects using the eval() function. So we could also do this:

def calculator():
    while True:
        intro = input('Hello! Please type * for multiplication, / for division, + for addition, - for subtraction, ** for exponents, and "s" for square root \n')
        intro = intro.capitalize()
        if intro not in ["*", "/", "+", "-", "**", "S"]:
            print ("that wasnt an option!")
            continue
        if intro != "S":
            num1 = input("Whats your first number \n")
            num2 = input("Whats your second number \n")
            print(
                eval(num1+intro+num2)
            )
            break
        else:
            num_sqr = int(input("What number do you wanna find the square root of \n"))
            print(math.sqrt(num_sqr))
            break

Michael Green
  • 719
  • 6
  • 15
0

Instead of checking if the command is s and then waiting till the end to execute the square root command, you can run the "s" or "S" command before asking the first or second number. This code will do`

import math

def calculator():
    while True:
        s = "s"
        intro = input('Hello! Please type * for multiplication, / for division, + for addition, - for subtraction, ** for exponents, and "s" for square root \n')
        if intro not in ["*", "/", "+", "-", "**", s.upper(), s]:
            print ("that wasnt an option!")
            continue
        if intro == s:
          num_sqr = int(input("What number do you wanna find the square root of \n"))
          print(math.sqrt(num_sqr))
          break

        if intro == s.upper():
            num_sqr = int(input("What number do you wanna find the square root of \n"))
            print(math.sqrt(num_sqr))
            break


        if intro != s:
           num1 = int(input("Whats your first number \n"))
           num2 = int(input("Whats your second number \n"))
        if intro != s.upper():
           num1 = int(input("Whats your first number \n"))
           num2 = int(input("Whats your second number \n"))  
        if intro == "*":
            print(num1 * num2)
            break
        elif intro == "/":
            print(num1/num2)
            break
        elif intro == "+":
            print(num1 + num2)
            break
        elif intro == "-":
            print(num1 - num2)
            break
        elif intro == "**":
            print(num1 ** num2)
            break
        
            
            
calculator()
Yappi
  • 43
  • 4
0

The problem with the code is that you did not actually format the input. You just have to upper() your input, and check all the condition with "S". This will also shorten your code.

import math


def calculator():
    while True:
        intro = input(
            'Hello! Please type * for multiplication, / for division, + for addition, - for subtraction, ** for exponents, and "s" for square root \n').upper()
        if intro not in ["*", "/", "+", "-", "**", "S"]:
            print("that wasnt an option!")
            calculator()
        if intro != "S":
            num1 = int(input("Whats your first number \n"))
            num2 = int(input("Whats your second number \n"))
        if intro == "*":
            print(num1 * num2)
            break
        elif intro == "/":
            print(num1/num2)
            break
        elif intro == "+":
            print(num1 + num2)
            break
        elif intro == "-":
            print(num1 - num2)
            break
        elif intro == "**":
            print(num1 ** num2)
            break
        elif intro == "S":
            num_sqr = int(
                input("What number do you wanna find the square root of \n"))
            print(math.sqrt(num_sqr))
            break


calculator()
Cheang Wai Bin
  • 133
  • 1
  • 7
0

You made two if statements with two different conditions and even if you enter "s" or "S" one of the if statements is going to evaluate to True all the time.

    # ONE OF THIS IF STATEMENT IS GOING TO BE TRUE ALL TIME WHEN ENTER "S" or "s"
    if intro != s:
       num1 = int(input("Whats your first number \n"))
       num2 = int(input("Whats your second number \n"))
    if intro != s.upper():     
       num1 = int(input("Whats your first number \n"))
       num2 = int(input("Whats your second number \n")) 

You can easily solve this by evaluating both conditions in only one if statement

    if intro != s and intro != s.upper():               #<-- if not "s" or "S" 
       num1 = int(input("Whats your first number \n"))
       num2 = int(input("Whats your second number \n"))
Diego Suarez
  • 901
  • 13
  • 16
0

Try changing this:

intro = input('Hello! Please type * for multiplication, / for division, + for addition, - for subtraction, ** for exponents, and "s" for square root \n')

into this:

intro = input('Hello! Please type * for multiplication, / for division, + for addition, - for subtraction, ** for exponents, and "s" for square root \n').lower()