Author | Message |
---|---|
| ![]() Python Guide on How to make a CalculatorWell this is a tutorial on how to make a calculator in "Python"(console) and you can deduce alot from this guide if you keep on reviewing it and practicing it.
-NEXT->NEXT-> Let it install
-Goto File->New Window -In the new Windows goto File->Save and goto your desktop and save it as "calc.py"
print "SYNTAX: 'multiply, add, divide'" - This prints the words in the "" on the console screen I did this because it shows the directions for the add,multiply, and divide operations. print "" - This prints nothing on the console screen I just wanted to space the words out(quick solution). operation = raw_input("Enter a operation here: ") - We need to make it so the console asks a question to the user(enter operation here: ) and they can enter a answer(you will see where this is useful later on). m = str("multiply") - "m" is a vairable and str("multiply") makes the word multiply a string(str).
if operation == m: m1 = input("Enter first number: ") m2 = input("Enter second number: ") m3 = str(m1 * m2) print "Your total is: " + m3 - The above code is going to take some explanation so just read this paragraph. The "if operation == m" means that if the operation equals m(multiply) then it will want a input number for the "input ("Enter first number: ")" and this is also the same for the second input number code. The "m3
=str(m1 * m2)" multiplys your first input(m1) with your second input(m2) and m3 equals the total. The "print "Your total is: " + m3" makes the screen print the words in "" and your total(m3) of your two inputs. - You have to do this three times just change the m1,m2,m3 values to d1,d2,d2 and a1,a2,a3. Yo also have to change the "*" sign in to "+(add) or /(divide). The last thing you have to "if operation == m" change the "m" to "a" or "d". EXAMPLE: if operation == a: a1 = input("Enter first number: ") a2 = input("Enter second number: ") a3 = str(a1 + a2) print "Your total is: " + a3 if operation == d: d1 = input("Enter first number: ") d2 = input("Enter second number: ") d3 = str(d1 / d2) print "Your total is: " + d3 Now just press f5 and the IDLE should come up with your program. You also had saved the source on your desktop as calc.py just double click that it should work as well if you want to edit the source left click on calc.py and click "edit with IDLE".
print "SYNTAX: 'multiply, add, divide'" print "" operation = raw_input("Enter a operation here: ") m = str("multiply") a = str("add") d = str("divide") if operation == m: m1 = input("Enter first number: ") m2 = input("Enter second number: ") m3 = str(m1 * m2) print "Your total is: " + m3 if operation == a: a1 = input("Enter first number: ") a2 = input("Enter second number: ") a3 = str(a1 + a2) print "Your total is: " + a3 if operation == d: d1 = input("Enter first number: ") d2 = input("Enter second number: ") d3 = str(d1 / d2) print "Your total is: " + d3 print 'Done' |