anyways, i have been wanting to make a tutorial for a while and thought this would be a great opportunity.
for a calculator, there are a few basic things that you are going to want to do
1. present the user with a menu to select a type of operation.
2. read the user input
3. get the numbers that the user wants to use
4. perform an operation using the input from the user
5. print the results
well doing the first part is pretty easy. in python to print you simply use
print 'whatever you want to print goes here'
so our first step can be done like so:
#menu for user to select an operation print '1. multiplication' print '2. division' print '3. addition' print '4. exponent' print '5. modulus' print '6. subtraction\n'
btw the \n is simply an escape sequence that drops the following text to the next line.
for our second step we must get the users input. this can be done using:
variable = raw_input('string that prompts the user for what you want')
in our particular situation we want to convert the input to an integer, we can do this like so:
#prompts user for input and saves the values to variables
operation = int(raw_input('What type of operation would you like to do?\n'))
first_number = int(raw_input('Please enter your first number to be computed\n'))
second_number = int(raw_input('Please enter your second number to be computer\n'))
the next step is to perform a certain operation to the two numbers that the user inputs, but it has to correlate to the operation that they selected from our menu. this can be achieved using if statements
if statements in python are pretty simple. they consist of the word 'if', a condition, a colon, and then what you want to do if the condition is satisfied. it looks something like this:
if operation == 1:
print first_number * second_number
so the rest of our if statements are pretty much common sense and when done they look like this:
if operation == 1:
print first_number * second_number
if operation == 2:
print first_number / second_number
if operation == 3:
print first_number + second_number
if operation == 5:
print first_number % second_number
if operation == 4:
print first_number ** second_number
if operation == 6:
print first_number - second_number
we are nearly done, but in python, it is good to add
raw_input("Press enter to quit")
to make sure that the user gets to see their results before the program ends.
when your all done your program will look similar to this:
#menu for user to select an operation
print '1. multiplication'
print '2. division'
print '3. addition'
print '4. exponent'
print '5. modulus'
print '6. subtraction\n'
#prompts user for input and saves the values to variables
operation = int(raw_input('What type of operation would you like to do?\n'))
first_number = int(raw_input('Please enter your first number to be computed\n'))
second_number = int(raw_input('Please enter your second number to be computer\n'))
print '\n'
if operation == 1:
print first_number * second_number
if operation == 2:
print first_number / second_number
if operation == 3:
print first_number + second_number
if operation == 5:
print first_number % second_number
if operation == 4:
print first_number ** second_number
if operation == 6:
print first_number - second_number
print '\n'
#prompts user to quit the program
raw_input("Press enter to quit")
hope this was a helpful tutorial. i kind of did it in a rush, so typos are likely. thanks you guys.




MultiQuote


|