print("Welcome to calculator.py")
print ("your options are :")
print ("")
print ("1) Addition")
print ("2)Subtraction")
print ("3) Multiplication")
print ("4)Division")
print ("Choose your option")
#this adds 2 numbers given
def add (a,b):
print(a, "+" ,b, "=" ,a+b)
#this subtracts 2 numbers given
def sub (a,b):
print(a, "-" ,b, "=" ,b-a)
#this multiplies 2 numbers given
def mul(a,b):
print(a, "*" ,b, "=" ,a*b)
#this divides 2 numbers given
def div(a,b):
def div(a, "/" ,b, "=" ,a/b)
#NOW THE PROGRAM REALLY STARTS ,AS CODE IS RUN
loop = 1
choice = 0
while loop == 1:
choice = menu()
if choice == 1:
add (input("Add this: "),input ("to this: "))
elif choice == 2:
sub(input("Subtract this: "),input ("from this: "))
elif choice == 3:
mul (input("Multiply this: "),input ("by this: "))
elif choice == 4:
div(input("Divide this: "),input ("by this: "))
elif choice ==5:
loop = 0
print ("Thank you for using calculator.py")
this is my code and the error is:
print(a, "+" ,b, "=" ,a+b)
^
IndentationError: expected an indented block
Process finished with exit code 1
and many others can anyone help me find all the errors???
can anyone help me???
In python, there are identation rules to know how the program should execute commands. This is the way python recognize when a function, a conditional statement or for, while statements start and end. In your case, for example, the elif statement should be at the same "level" as your initial if statement start:
print("Welcome to calculator.py")
print ("your options are :")
print ("")
print ("1) Addition")
print ("2)Subtraction")
print ("3) Multiplication")
print ("4)Division")
print ("Choose your option")
#this adds 2 numbers given
def add (a,b):
print(a, "+" ,b, "=" ,a+b)
#this subtracts 2 numbers given
def sub (a,b):
print(a, "-" ,b, "=" ,b-a)
#this multiplies 2 numbers given
def mul(a,b):
print(a, "*" ,b, "=" ,a*b)
#this divides 2 numbers given
def div(a,b):
def div(a, "/" ,b, "=" ,a/b)
#NOW THE PROGRAM REALLY STARTS ,AS CODE IS RUN
loop = 1
choice = 0
while loop == 1:
choice = menu()
if choice == 1:
add (input("Add this: "),input ("to this: "))
elif choice == 2:
sub(input("Subtract this: "),input ("from this: "))
elif choice == 3:
mul (input("Multiply this: "),input ("by this: "))
elif choice == 4:
div(input("Divide this: "),input ("by this: "))
elif choice ==5:
loop = 0
In python blocks are defined by indentation. After defining function you should have a indentation(tab).
Pretty much all of the indentation is wrong, not just that one place:
print("Welcome to calculator.py")
print("your options are :")
print("")
print("1) Addition")
print("2)Subtraction")
print("3) Multiplication")
print("4)Division")
print("Choose your option")
#this adds 2 numbers given
def add(a,b):
print(a, "+", b, "=" , a+b)
#this subtracts 2 numbers given
def sub(a,b):
print(a, "-", b, "=" , b-a)
#this multiplies 2 numbers given
def mul(a,b):
print(a, "*", b, "=" , a*b)
#this divides 2 numbers given
def div(a,b):
print(a, "/", b, "=", a/b)
#NOW THE PROGRAM REALLY STARTS, AS CODE IS RUN
loop = 1
choice = 0
while loop == 1:
choice = menu()
if choice == 1:
add(input("Add this: "), input("to this: "))
elif choice == 2:
sub(input("Subtract this: "), input("from this: "))
elif choice == 3:
mul(input("Multiply this: "), input("by this: "))
elif choice == 4:
div(input("Divide this: "), input("by this: "))
elif choice ==5:
loop = 0
print ("Thank you for using calculator.py")
Related
This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 4 years ago.
my output is the menu() function prints only all over again and again, don't matter what the input is it won't get to the "else if" statement #Python...........................................................................
def menu():
#print what options you have
print ("Welcome to calculator.py")
print ("your options are:")
print (" ")
print ("1) Addition")
print ("2) Subtraction")
print ("3) Multiplication")
print ("4) Division")
print ("5) exposion")
print ("6) Quit calculator.py")
print (" ")
return input ("Choose your option: ")
# this adds two numbers given
def add(a,b):
print (a, "+", b, "=", a + b)
# this subtracts two numbers given
def sub(a,b):
print (b, "-", a, "=", b - a)
# this multiplies two numbers given
def mul(a,b):
print (a, "*", b, "=", a * b)
# this divides two numbers given
def div(a,b):
print (a, "/", b, "=", a / b)
# HERE IS MY CODE
def dinami(base, exponent):
if y == 1:
print (base, "*" ,power,"(",base,",", exponent," - 1) = ",base)
if y != 1:
print (base, "*" ,power,"(",base,",", exponent," - 1) = ",base * power(base, exponent - 1))
# NOW THE PROGRAM REALLY STARTS, AS CODE IS RUN
loop = 1
while loop == 1:
choice = menu()
if choice == 1:
add(input("Add this: "),input("to this: "))
print ("hi")
elif choice == 2:
sub(input("Subtract this: "),input("from this: "))
elif choice == 3:
mul(input("Multiply this: "),input("by this: "))
elif choice == 4:
div(input("Divide this: "),input("by this: "))
elif choice == 5:
dinami(input("expone this: "),input("to this: "))
elif choice == 6:
loop = 0
print ("Thankyou for using calculator.py!")
input() returns a string, so if you're comparing int with string i.e '1' == 1, which always returns False
https://docs.python.org/3/library/functions.html#input
def snt(a1, b1):
snt0 = a1+b1
return snt0
def mnt(a1, b1):
mnt0 = a1*b1
return mnt0
print("Hello to my console program")
print("This is a basic sum multiplication calculator ")
i=1
while (i>0):
clicky=input("For sum click on 1 for multiplcation click on 2: ")
if clicky==1:
a=int(input("enter a"))
b=int(input("enter b"))
f=snt(a,b)
print(a ,"+", b ,"=",f)
input("Click to go out")
break
elif clicky==2:
a=int(input("enter a"))
b=int(input("enter b"))
f=mnt(a,b) before
print(a ,"*", b ,"=", f)
input("Click to go out")
break
else:
i += 1
I can't understand why it's not working i know something is wrong with the while loop but i can't find it.
Note: I"ve just started learning python !
input() returns a string, eg. "1". you need to cast it to an integer for your comparison to work.
clicky = input(...)
clicky = int(clicky)
Here is the correct Answer:
def snt(a1, b1):
snt0 = a1+b1
return snt0
def mnt(a1, b1):
mnt0 = a1*b1
return mnt0
print("Hello to my console program")
print("This is a basic sum multiplication calculator ")
i=1
while (i > 0):
clicky = input("For sum click on 1 for multiplcation click on 2: ")
clicky = int(clicky)
if clicky == 1:
a = int(input("enter a: "))
b = int(input("enter b: "))
f = snt(a,b)
print(a ,"+", b ,"=",f)
input("Click to go out")
break
elif clicky == 2:
a = int(input("enter a: "))
b = int(input("enter b: "))
f = mnt(a,b)
print(a ,"+", b ,"=", f)
input("Click to go out")
break
else:
i += 1
The input on the variable clicky requires a cast, as you did correctly later for the variables a and b.
def snt(a1, b1):
snt0 = a1 + b1
return snt0
def mnt(a1, b1):
mnt0 = a1 * b1
return mnt0
print("Hello to my console program")
print("This is a basic sum multiplication calculator ")
i = 1
while (i > 0):
clicky = int(input("For sum click on 1 for multiplcation click on 2: "))
if clicky == 1:
a = int(input("enter a: "))
b = int(input("enter b: "))
f = snt(a, b)
print(a, "+", b, "=", f)
input("Click to go out")
break
elif clicky == 2:
a = int(input("enter a: "))
b = int(input("enter b: "))
f = mnt(a, b)
print(a, "*", b, "=", f)
input("Click to go out")
break
else:
i += 1
Next time, when you open a ticket please try to make some effort to clarify what it is not working, what it is the error or the unexpected behavior you get.
File "first.py", line 37
elif(operation == "quadratic formula")
IndentationError: unindent does not match any outer indentation level
I really don't know what the problem is with my code. Can someone please help?
def add(var1, var2):
return var1 + var2
def sub(num1, num2):
return num1 - num2
def div(num1, num2):
return num1/num2
def mul(num1,num2):
return num1*num2
def equation1():
import math
a = int(raw_input("please enter your a in the equation: "))
b = int(raw_input("please enter your b in the equation: "))
c = int(raw_input("please enter your c in the equation: "))
d = b**2-4*a*c
if(d < 0):
print "This equation has no real solution"
elif(d == 0):
x = (-b+math.sqrt(b**2-4*a*c))/(2*a)
print "This equation has one solutions: ", x
else:
x1 = (-b+math.sqrt(b**2-4*a*c))/(2*a)
x2 = (-b-math.sqrt(b**2-4*a*c))/(2*a)
print "This equation has two solutions"
print "The first soluition: ", x1
print "And the second solution", x2
def main():
operation = raw_input("what do you want to do (+,-,/,*) or quadratic
formula? ")
if(operation != '+' and operation != '-' and operation != '/' and operation != '*' and operation != 'quadratic formula'):
print 'You must put a valid operation'
elif( operation == "quadratic formula" ):
equation1()
else:
var1 = int(raw_input("enter the first number: " ))
var2 = int(raw_input("enter the second number: "))
if(operation == '+'):
print (add(var1, var2))
elif(operation == '/'):
print (div(var1, var2))
elif(operation == '-'):
print (sub(var1, var2))
The following shows your code in my text editor with tabs and space characters made visible and shown in the color red. As you can see, it indicates that you're using a mixture of them in it—and that's confusing the Python interpreter. Best to stick with one or the other, although I think 4 spaces are the best (and most portable).
You may be able to configure you editor to automatically convert tabs into the right number of spaces for you.
Update:
Since you still seem to be having problems, here's a version of your code in which I have removed the tab characters so now its indentation is comprised exclusively of 4 space characters for each level. See if you have better luck with it.
def add(var1, var2):
return var1 + var2
def sub(num1, num2):
return num1 - num2
def div(num1, num2):
return num1/num2
def mul(num1,num2):
return num1*num2
def equation1():
import math
a = int(raw_input("please enter your a in the equation: "))
b = int(raw_input("please enter your b in the equation: "))
c = int(raw_input("please enter your c in the equation: "))
d = b**2-4*a*c
if(d < 0):
print "This equation has no real solution"
elif(d == 0):
x = (-b+math.sqrt(b**2-4*a*c))/(2*a)
print "This equation has one solutions: ", x
else:
x1 = (-b+math.sqrt(b**2-4*a*c))/(2*a)
x2 = (-b-math.sqrt(b**2-4*a*c))/(2*a)
print "This equation has two solutions"
print "The first soluition: ", x1
print "And the second solution", x2
def main():
operation = raw_input("what do you want to do (+,-,/,*) or quadratic formula? ")
if(operation != '+' and operation != '-' and operation != '/' and operation != '*' and operation != 'quadratic formula'):
print 'You must put a valid operation'
elif( operation == "quadratic formula" ):
equation1()
else:
var1 = int(raw_input("enter the first number: " ))
var2 = int(raw_input("enter the second number: "))
if(operation == '+'):
print (add(var1, var2))
elif(operation == '/'):
print (div(var1, var2))
elif(operation == '-'):
print (sub(var1, var2))
Here is my code: I'm not sure what the problem is. It says invalid syntax on "def sub". I've looked everywhere and as far as I can tell, it is properly formatted for python 3
def add():
print ("Enter the two numbers to Add")
A=int(raw_input("Enter A: "))
B=int(raw_input("Enter B: "))
return A + B
def sub():
print ("Enter the two numbers to Subtract")
A=int(raw_input("Enter A: "))
B=int(raw_input("Enter B: "))
return A - B
def mul():
print ("Enter the two numbers to Multiply")
A=int(raw_input("Enter A: "))
B=int(raw_input("Enter B: "))
return A * B
def div():
print ("Enter the two number to Divide")
A=float(raw_input("Enter A: "))
B=float(raw_input("Enter B: "))
return A / B
print ("1: ADD")
print ("2: SUBTRACT")
print ("3: MULTIPLY")
print ("4: DIVIDE")
print ("0: QUIT")
while True:
CHOICE = int(raw_input("ENTER THE CORRESPONDING NUMBER FOR CALCULATION "))
if CHOICE == 1:
print ('ADD TWO NUMBERS:')
print add()
elif CHOICE == 2:
print ('SUBTRACT TWO NUMBERS')
print sub()
elif CHOICE == 3:
print ('MULTIPLY TWO NUMBERS')
print mul()
elif CHOICE == 4:
print ("DIVIDE TWO NUMBERS")
print div()
elif CHOICE == 0:
exit()
else:
print ("The value Enter value from 1-4")
All the prints like:
print add()
are missing the parenthesis. They should be:
print(add())
Same for all the prints
i am novice to python and i have stuck at one point, though its a simple program
I have used 'Return False' to move out of the function but i want to move out of my function completely. How it can be done.
Also if i want to run this script from python shell, how it can be done.
def menu():
print "calculator using functions"
print "Choose your option:"
print " "
print "1) Addition"
print "2) Subtraction"
print "3) Multiplication"
print "4) Division"
print "5) Quit calculator.py"
print " "
return input ("Choose your option: ")
def add(a,b):
print a, "+", b, "=", a + b
print " Do you want to continue: "
decide=raw_input("yes or no: ")
if decide== "no" or decide== 'n':
print(" You have exited ")
return False
elif decide=='yes' or decide== 'y':
menu()
else:
print "wrong choice!!!"
return False
# this subtracts two numbers given
def sub(a,b):
print b, "-", a, "=", b - a
# this multiplies two numbers given
def mul(a,b):
print a, "*", b, "=", a * b
# this divides two numbers given
def div(a,b):
print a, "/", b, "=", a / b
loop = 1
choice = 0
while loop == 1:
choice = menu()
if choice == 1:
add(input("Add first No: "),input("Add second No: "))
elif choice == 2:
sub(input("Add first No: "),input("Add second No: "))
elif choice == 3:
mul(input("Add first No: "),input("Add second No: "))
elif choice == 4:
div(input("Add first No: "),input("Add second No: "))
elif choice == 5:
loop = 0
print "End of program!"
You want to use exit. It exits from the program.
import sys
def spam():
.
.
.
if some_condition:
sys.exit(0) # exits from the program
.
.
.
You don't have to explicitly return anything to exit a function. When the interpreter reaches the end of the function block, the function exits.
On the command line type:
python myprogram.py
to run the program from prompt, if you want to use a specific python shell (other than bash or cmd) then you need to look to the documentation of that specific shell (e.g http://www.dreampie.org/).
To exit from a function use:
return
To exit from a program use:
import sys
sys.exit(0)