Python Codebit Question?



I need some help debugging my code. I am trying to create a calculator in python. I can't figure out what I'm doing wrong. Here is my code:

def calculate(n):
# Prompts user for an operation.
operation = raw_input("Pick an operation. Type 'ex' for exponentiation, 'add' for addition, 'sub' for subtraction, 'times' for multiplication, 'div' for division, and 'mod' for modulo: "
# When using exponentiation:
if operation == 'ex':
power = raw_input("Pick an exponent: ")
p = power
return n**p
# When using addition:
elif operation == 'add':
addend = raw_input("What would you like to add %s to?" % (n))
add = addend
return n + add
# When using subtraction:
elif operation == 'sub':
minuend = raw_input("What would you like to subtract %s from?" % (n))
mino = minuend
return mino - n
# When using multiplication:
elif operation == 'times':
factor = raw_input("What would you like to multiply %s by?" % (n))
fact = factor
return n * fact
# When using division:
elif operation == 'div':
denom = raw_input("What would you like to divide %s by?" % (n))
den = denom
return n / den
# When finding remainders (modulo):
elif operation == 'mod':
modulus = raw_input("What would you like to find the remainder of by dividing %s by?")
mod = modulus
return n % mod
# When other operation is given / no operation:
print "I'm sorry, could you please try again? Run this code again."

Type "print calculate(number)" and follow instructions:

print calculate(3)



Please re-edit your Post

  • leave one blank-line above of your code
  • select your code in the Post
  • then =click= on the </>-symbol-of-this-editor

Your code will then be in a pre-code state
and you will be able to make/present the proper indentations.

or even better use