# New to Programming, need help with python if statement

 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 http://gyazo.com/013b9e1e764ec039e7552d17b740a284``#If statement 2#Definition for addingdef add(num1, num2):      return num1 + num2#Definition for subtractingdef sub(num1, num2):      return num1 - num2#Definition for multiplyingdef mul(num1, num2):      return num1 * num2#Definition for dividingdef div(num1, num2):      return num1 / num2#Definition for powersdef pow(num1, num2):      return num1 ** num2#Main functiondef 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]                  else:            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))            else:                  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"):                  exit()            elif(wait == "continue"):                  main()            else:                  print("Invalid statement")                  main()``

## Replies

 A B · April 27, 2015 Try using ``isinstance(variable, type)``to check whether a variable is a specific typeExample:``x = 'string'if isinstance(x, int) == False: print('x Is Not An Integer')# Output:x Is Not An Integer`` Stian OK · April 27, 2015 ``age = 17if isinstance(age, int):    print("age is an integer")else:    print("age is not an integer")`` Caleb Carlson · April 27, 2015 Ok I will try these and get back 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

## Python

130,904 followers