New to Programming, need help with python if statement

+1 Caleb Carlson · April 26, 2015
So here's my code so far, I'm trying to create a basic calculator. What I need help with is the IF statement that checks whether or not the user entered an integer, and if not, then it prints "You must enter a number" and goes back and restarts main()

I tried to use while statements, but couldn't quite figure out how they worked. The problem code is bold below.
EDIT: Would not indent on web, so here's a sample screenshot of IDLE

#If statement 2

#Definition for adding

def add(num1, num2):

      return num1 + num2

#Definition for subtracting

def sub(num1, num2):

      return num1 - num2

#Definition for multiplying

def mul(num1, num2):

      return num1 * num2

#Definition for dividing

def div(num1, num2):

      return num1 / num2

#Definition for powers

def pow(num1, num2):

      return num1 ** num2

#Main function

def main():

      print("Calculate by entering the first number below")

      [b]varOne = int(input("First number: "))
      if(type(varOne) != int):
            print("You must enter a number.")[/b]



            operation = input("Choose an operation (+ - * / ^): ")

            if((operation != "+" and operation != "-") and (operation != "*" and operation != "/" and operation != "^")):

                  print("Invalid operation.")

            elif(operation == "+"):

                  varTwo = int(input("Second number: "))

                  print(add(varOne, varTwo))

            elif(operation == "-"):

                  varTwo = int(input("Second number: "))

                  print(sub(varOne, varTwo))

            elif(operation == "*"):

                  varTwo = int(input("Second number: "))

                  print(mul(varOne, varTwo))

            elif(operation == "^"):

                  varTwo = int(input("Second number: "))

                  print(pow(varOne, varTwo))


                  varTwo = int(input("Second number: "))

                  print(div(varOne, varTwo))

            wait = str(input("Press type close to close, or continue to try again: "))

            if(wait == "close"):


            elif(wait == "continue"):



                  print("Invalid statement")



Post a Reply


Oldest  Newest  Rating
0 A B · April 27, 2015
Try using 
isinstance(variable, type)

to check whether a variable is a specific type
x = 'string'
if isinstance(x, int) == False:
print('x Is Not An Integer')

# Output:
x Is Not An Integer
0 Stian OK · April 27, 2015
age = 17
if isinstance(age, int):
   print("age is an integer")
   print("age is not an integer")
0 Caleb Carlson · April 27, 2015
Ok I will try these and get back
+1 Amine Kouki · April 28, 2015
In your algorithm, you did not test the second variable's type.

I suggest that you enter the whole operation then do the whole treatment later.

The simple way is to use three successive inputs to enter; let's say var1, operator and var2. Then it will be easier to test the validity of your inputs..

The next stage would be to enter the whole operation in one input; let's say "5 + 4" then extract these elements one by one then calculate the result or throw something like "Invalid operation"
  • 1



This section is all about snakes! Just kidding.

Bucky Roberts Administrator