![]() |
|
[Python] [TUT]How to write a calculator in Python 2.6[TUT] - Printable Version +- Hack Forums (https://hackforums.net) +-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444) +--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151) +---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118) +---- Thread: [Python] [TUT]How to write a calculator in Python 2.6[TUT] (/showthread.php?tid=507091) Pages: Pages
1
2
|
[TUT]How to write a calculator in Python 2.6[TUT] - Screwbacca - Jun 27, 2010 Hey HackForums and Python coders! I'm Jedz and I'm gonna teach you how to write a calculator in Python. This is aimed mostly towards beginners, but anybody can read and benefit from it. :) Anyway lets get on with it. Any text in THIS COLOR is a new function that you will be using. Okay so lets get started. First we need to define our calculator. We do this using this code: def Calc(): What this allows us to do is type whatever you named your calculator (I named mine Calc) and if you type in Code Calc()Now we need our functions. And on a sidenote, I will put the whole calculator, finished and all, at the bottom as a reference. This is easy, we just print the functions and what number they correspond to in order for the user to access its functions. Code print "Please Select a function"AWESOME! WE GOT OUR FUNCTIONS! Now you'll be asking how do we actually use input to get the functions to actually do their job. You got it. We use input So here is the code for our input function = input("> ") Okay so what this does is allow the user to enter a number and it will actually do the task we wanted. OH MY GOD THATS AWESOME. Not really. OK. We got out user input down. So what about the actual math part? Well before we can get to that we have to use the functions: if, elif, else What these functions do is if a user inputs X, then X will happen. if the use enters something else, then X will happen. elif is used when there are more than two choices. First I want to distinguish between something. It's between the = and the ==. The single equal sign is used when SETTING A VALUE TO SOMETHING. The double equal is used when saying something IS TRUE TO SOMETHING ELSE. Now that we have that down, lets get on to our conditional statements and computing the problems. We have a situation in which we want addition. To do this section it would be: Code if function==1:If you understand this you will be able to use this format with every function except for a few that I'll name in a bit. Just substitute the variables and set the values for whatever you like :) Now as for those special functions, you will need to import a module. This module is called math. This module includes all the functions other than basic add, subtract,multiply, divide, and exponents(BTW, the syntax for exponents is the double asterik . So 52 = 25.). So you would need to use Code import mathCode dir(math)Code help(math)Now, in order to use these you would do Code math.sqrt(x)***IN ORDER TO USE AN IMPORTED MATH MODULE YOU MUST INCLUDE "math" FOLLOWED BY A PERIOD AND WHATEVER FUNCTION IN THE MODULE YOU WANT TO USE*** So once you figured everything out, your final product should be someone similar to mine: Code def FF():For some reason, the indents are messed up on my code. The standard indent for IDLE is 4 spaces. Keep in mind. I you understood and enjoyed my tutorial :) I'll be back for more. Please comment! Jedz * - Paranoia - Jun 27, 2010 Very nice Jedz! Keep up the great TUT's. * - Screwbacca - Jun 27, 2010 Thanks Paranoia! I may write one on wxPython soon if enough people ask me :) * - Kalvaxas - Jun 27, 2010 I'm almost 100% sure this is stolen from http://docs.python.org/ I swear I read that same thing, just a few re-worded words. * - Screwbacca - Jun 27, 2010 I've had some code stolen before and I know how it feels. It sucks. So was my code or my instruction "stolen"? * - Screwbacca - Jun 27, 2010 Okay. I know I didnt steal it and thats all that matters. And its true. * - Buzz Lightyear - Jun 27, 2010 tahnks a bunch dude. really helped me out * - Screwbacca - Jun 27, 2010 No problem Buzz. Glad I could help :D * - Xyk™ - Jun 28, 2010 I don't think this is stolen. (btw: nice tut, but the code doesn't looks too "pythonic") * - Screwbacca - Jun 28, 2010 Haha i tried to make it easy to understand. This was my first 'real' program that actually served a purpose. I'm gonna write a wxPython tut maybe later tonight or tomorrow. Does any one have any tut requests? * - M3G - Nov 11, 2010 thnx man helped quite abit * - DogTired - Jul 7, 2013 Thx man :) Helped a python noob a lot :D Regards DaBootO * - T0ucaNSaM - Aug 19, 2013 Appreciate that you took the time to write out this TUT hopefully more will come bro !! * - ImportUsername - Aug 19, 2013 Wow priate_toucan... What is with you and necroing posts? Did you just learn Python and are now so excited that you have to comment on everything? Or are you just a troll/asshole? * - eoNe - Aug 20, 2013 Very very nice tutorial thank you bro^^ |