1,105,271 Community Members

Basic Python Program

Member Avatar
RockJake28
Light Poster
27 posts since Dec 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 4 [?]
Skill Endorsements: 0 [?]
 
1
 

I have this code so far

#Add two digits
def add(a,b):
    return a+b

#Subtract two digits
def sub(a,b):
    return a-b

#Multiply two digits
def mul(a,b):
    return a*b

#Divide two digits
def div(a,b):
    return a/b

#returns an integer that the user enters
def getCommand():
    cmd = input("Choose: ")
    return cmd

#Execute the command given
def execute(cmd):   
    a = input("Provide first number: ")
    b = input("Provide second number: ")

    if cmd == 1:
        print (add(a,b))

    if cmd == 2:
        print (sub(a,b))

    if cmd == 3:
        print (mul(a,b))

    if cmd == 4:
        print (div(a,b))

#displays the menu
def dispMenu():
    print ("1. Add two numbers")
    print ("2. Subtract two numbers")
    print ("3. Multiply two number")
    print ("4. Divide two numbers")


#main()    
while True:
    dispMenu()
    cmd = getCommand()
    execute(cmd)

However, when its ran, it gives the following:

  1. Add two numbers
  2. Subtract two numbers
  3. Multiply two number
  4. Divide two numbers
    Choose: 1
    Provide first number: 1
    Provide second number: 2
  5. Add two numbers
  6. Subtract two numbers
  7. Multiply two number
  8. Divide two numbers
    Choose:

It doesnt give me the answer. Any ideas as to why?
(This is my second program in python)

Member Avatar
RockJake28
Light Poster
27 posts since Dec 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 4 [?]
Skill Endorsements: 0 [?]
 
1
 

Problem solved, I needed the inputs to be parsed as ints.

Question Self-Answered as of 1 Year Ago
Member Avatar
Lardmeister
Posting Virtuoso
1,966 posts since Mar 2007
Reputation Points: 434 [?]
Q&As Helped to Solve: 111 [?]
Skill Endorsements: 8 [?]
 
0
 

A common mistake!

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: