Page 1 of 1

## calculator

### #1 gibson.nathan

• D.I.C Regular

Reputation: 8
• Posts: 294
• Joined: 06-October 09

Posted 25 May 2010 - 10:50 PM

i just started programming in python and am just getting the hang of it myself. one of the things i like to do to gain a little confidence when i first start with a new language is to make a simple calculator. it uses alot of basic things to get you going like, user input, if statements, and printing. its useful to get a feel for the syntax.

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.
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 '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'))
```

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.

```#menu for user to select an operation
print '1. multiplication'
print '2. division'
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'))

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.

Is This A Good Question/Topic? 1

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }