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 http://gyazo.com/013b9e1e764ec039e7552d17b740a284


#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]


            


      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()


Post a Reply

Replies

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


to check whether a variable is a specific type
Example:
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")
else:
   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

Python

107,132 followers
About

This section is all about snakes! Just kidding.

Links
Moderators
Bucky Roberts Administrator