# Calculator (Is this code ok?)

Page 1 of 1

## 12 Replies - 1614 Views - Last Post: 14 April 2011 - 01:01 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=227655&amp;s=6c035b7db78f9d6411d4c62e0fdeaa6d&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Mossypne

Reputation: 7
• Posts: 133
• Joined: 18-August 10

# Calculator (Is this code ok?)

Posted 14 April 2011 - 12:01 PM

Hey,

After learning python for a while i decided to try out what i learnt, so i decided to try and make a simple calculator.

This is what i got:

```def show_menu():
print "**********Calculator**********"
print "2.Subtraction\n"
print "3.Multiplication\n"
print "4.Division\n"
print "5.Quit\n"

num1 = input("Enter your first number: ")
num2 = input("Enter your second number: ")
num3 = num1 + num2
print num3

def sub():
num1 = input("Enter your first number: ")
num2 = input("Enter your second number: ")
num3 = num1 - num2
print num3

def mul():
num1 = input("Enter your first number: ")
num2 = input("Enter your second number: ")
num3 = num1 * num2
print num3

def div():
num1 = input("Enter your first number: ")
num2 = input("Enter your second number: ")
num3 = num1 / num2
print num3

running = 1

while running:
if selection == 1:
elif selection == 2:
sub()
elif selection == 3:
mul()
elif selection == 4:
div()
elif selection == 5:
quit

```

Is this code ok?? Was there a better way/easier way that i could of done it.

I'm going to keep going with this my next plan is to try and learn wxpython and create an interface for it.

Is This A Good Question/Topic? 0

## Replies To: Calculator (Is this code ok?)

### #2 atraub

• Pythoneer

Reputation: 828
• Posts: 2,235
• Joined: 23-December 08

## Re: Calculator (Is this code ok?)

Posted 14 April 2011 - 12:07 PM

This is good. As 1 suggestion, I would suggest getting rid of the running variable and change your while loop to read while True:. All in all this is a good attempt at a calculator program

### #3 Mossypne

Reputation: 7
• Posts: 133
• Joined: 18-August 10

## Re: Calculator (Is this code ok?)

Posted 14 April 2011 - 12:11 PM

Thanks you.

You have been a great help with my problems.

### #4 Nallo

• D.I.C Regular

Reputation: 165
• Posts: 258
• Joined: 19-July 09

## Re: Calculator (Is this code ok?)

Posted 14 April 2011 - 12:21 PM

There is always room for improvement.
Looking at your code you may realize, that the add, sub, mul, div function all have the same two first lines. You are repeating yourself. Why not make that an own function and call it from within add, mul, sub or div:
```def get_2_numbers():
num1 = input("Enter your first number: ")
num2 = input("Enter your second number: ")
return num1, num2

num1, num2 = get_2_numbers()
num3 = num1 + num2
print num3

```

This post has been edited by Nallo: 14 April 2011 - 12:22 PM

### #5 Mossypne

Reputation: 7
• Posts: 133
• Joined: 18-August 10

## Re: Calculator (Is this code ok?)

Posted 14 April 2011 - 12:26 PM

Nallo, on 14 April 2011 - 12:21 PM, said:

There is always room for improvement.
Looking at your code you may realize, that the add, sub, mul, div function all have the same two first lines. You are repeating yourself. Why not make that an own function and call it from within add, mul, sub or div:
```def get_2_numbers():
num1 = input("Enter your first number: ")
num2 = input("Enter your second number: ")
return num1, num2

num1, num2 = get_2_numbers()
num3 = num1 + num2
print num3

```

Thank you for pointing that out. I will make the changes to it to try and improve it.

I'm now trying to figure out how i can make it so that they can use unlimited amount of numbers not just two.

### #6 Nallo

• D.I.C Regular

Reputation: 165
• Posts: 258
• Joined: 19-July 09

## Re: Calculator (Is this code ok?)

Posted 14 April 2011 - 12:29 PM

Be warned: unlimited ammount of numbers is fine for mul and add. But not for div and sub.

### #7 baavgai

• Dreaming Coder

Reputation: 7113
• Posts: 14,828
• Joined: 16-October 07

## Re: Calculator (Is this code ok?)

Posted 14 April 2011 - 12:32 PM

Looks fine. Now, play with how you organize it.

Look at where code repeats and try to figure out of you can put it in a def. You ask for two numbers ever time...
```def getNums():
num1 = input("Enter your first number: ")
num2 = input("Enter your second number: ")
return (num1, num2)

(num1, num2) = getNums()
print num1 + num2

def sub():
(num1, num2) = getNums()
print num1 + num2

```

In python, you can also pass a function like variable. So, another attack can be:
```def operNums(f):
num1 = input("Enter your first number: ")
num2 = input("Enter your second number: ")
print f(num1, num2)

def f(num1, num2): return num1 + num2
operNums(f)

def sub():
def f(num1, num2): return num1 - num2
operNums(f)

```

Knowing that you can do this, you can actually attach the functions to the menu items and loop through them.

I'll leave that to you. Have fun.

### #8 atraub

• Pythoneer

Reputation: 828
• Posts: 2,235
• Joined: 23-December 08

## Re: Calculator (Is this code ok?)

Posted 14 April 2011 - 12:35 PM

Here's a little fooling around I did that may be helpful...

```>>> y = raw_input("")
5 23 656 343 232
>>> y
'5 23 656 343 232'
>>> z = y.split() #split the string on the spaces into a list of strings
>>> z
['5', '23', '656', '343', '232']
>>> a = [int(i) for i in z] #Build a list by going through each item in z and casting it to an int
>>> a
[5, 23, 656, 343, 232]
>>> sum(a) #sum the values in my list
1259

```

### #9 Mossypne

Reputation: 7
• Posts: 133
• Joined: 18-August 10

## Re: Calculator (Is this code ok?)

Posted 14 April 2011 - 12:43 PM

Thanks for all your help. You guys really are awesome.

atraub i dont understand that.

### #10 atraub

• Pythoneer

Reputation: 828
• Posts: 2,235
• Joined: 23-December 08

## Re: Calculator (Is this code ok?)

Posted 14 April 2011 - 12:50 PM

Haha it's ok, nobody understands me...

Care to be more specific about which part is tripping you up?

### #11 Mossypne

Reputation: 7
• Posts: 133
• Joined: 18-August 10

## Re: Calculator (Is this code ok?)

Posted 14 April 2011 - 12:53 PM

I just don't understand what its meant to be doing.

It's actually quite easy to add new things to my calculator. Managed to add powers in a matter of seconds

This post has been edited by Mossypne: 14 April 2011 - 12:56 PM

### #12 atraub

• Pythoneer

Reputation: 828
• Posts: 2,235
• Joined: 23-December 08

## Re: Calculator (Is this code ok?)

Posted 14 April 2011 - 12:56 PM

It's just some tom foolery, but the concepts may prove valuable

On line 1, the user enters numbers separated by a space... he can enter 2 numbers or two thousand. As many as he wants!

On line 5, we take that string of numbers and we turn it into a list
holding individual numbers (although they're strings).

Line 8 is a little tricky. We take that list of strings and we build a new list comprised of the same numbers in int form.

On line 11, we use the sum command to get the total value of all the numbers in the list added together.

Perhaps these concepts may prove valuable in expanding your calculator... perhaps not. Who knows?

This post has been edited by atraub: 14 April 2011 - 12:57 PM

### #13 Mossypne

Reputation: 7
• Posts: 133
• Joined: 18-August 10

## Re: Calculator (Is this code ok?)

Posted 14 April 2011 - 01:01 PM

Ah i understand it now. Its a way of adding multiple numbers together.