Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

My first program

I've been doing a lot lessons on Code Academy, but I get frustrated since some of the compilers reject answers that return the same results. So, I decided to breakaway for a bit and create my own program with the tools that I have learned which is a calculator. It doesn't run correctly. When it is at the menu the if/elif/else statements do not work. The program seems to ignore the input . So, here is my first program code...

import math
user_choice=(">>")
def add():
    print "What two numbers would you like to add?"
    a= int(raw_input(user_choice))                     
    b= int(raw_input(user_choice))
    c= a + b
    print c
def sub():
    print "What two numbers would you like to subtract?"
    a=int(raw_input(user_choice))
    b=int(raw_input(user_choice))
    c=a-b
    print c
def mult():
    print "What two numbers would you like to multiply?"
    a=int(raw_input(user_choice))
    b=int(raw_input(user_choice))
    c= a*b
    print c
def div():
    print "What two numbers would you like to divide?"
    a=int(raw_input(user_choice))
    b=int(raw_input(user_choice))
    c=a/b
    print c
def exp():
    print "What number would you like to power?"
    a=int(raw_input(user_choice))
    print "By what number would you like it to be powered to?"
    b=int(raw_input(user_choice))
    c= math.pow(a,b)
    print  c
def square():
    print "What number would you like to square root?"
    a=int(raw_input(user_choice))
    b=math.sqrt(a)
    print b

print "+---------------------------+"   
print "|   Welcome to my basic     |"
print "|    calculator!         |"
print "|                           |"
print "|What would you like to do? |"
print "|                |"
print "|1: Addition         |"
print "|2: Subtraction          |"
print "|3: Multiplication       |"
print "|4: Division         |"
print "|5: Exponents            |"
print "|6: Square Root          |"
print "|7: Quit         |"
print "|                           |"
print "+---------------------------+"

if int(raw_input(user_choice)) ==  "1":
     add()

elif int(raw_input(user_choice)) == "2":
     sub()

elif int(raw_input(user_choice)) == "3":
     mult()

elif int(raw_input(user_choice)) == "4":
     div()

elif int(raw_input(user_choice)) == "5":
     exp()

elif int(raw_input(user_choice)) == "6":
     square()

elif int(raw_input(user_choice)) == "7":
     exit()

else:
    print "Sorry, I didn't understand your entry.Try entering a value 1-7"

there is no "if error" code yet, but I am just adamant about getting it to work. All of the functions work. Just can't get the options to work.

like image 586
Atlas Avatar asked Dec 29 '25 23:12

Atlas


1 Answers

int(rawinput()) will return an integer, which will not be == to a string like "1". Remove the int() from them and it should work.

like image 155
Paul Becotte Avatar answered Dec 31 '25 14:12

Paul Becotte



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!