12 Replies - 1180 Views - Last Post: 14 April 2011 - 01:01 PM Rate Topic: -----

#1 Mossypne  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • 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 "1.Addition\n"
    print "2.Subtraction\n"
    print "3.Multiplication\n"
    print "4.Division\n"
    print "5.Quit\n"
    return input("Please enter selection:" )

def add():
    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:
    selection = show_menu()
    if selection == 1:
        add()
    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  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • 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 :)
Was This Post Helpful? 1
  • +
  • -

#3 Mossypne  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#4 Nallo  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 163
  • View blog
  • Posts: 255
  • 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

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


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

Was This Post Helpful? 0
  • +
  • -

#5 Mossypne  Icon User is offline

  • D.I.C Head
  • member icon

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

Re: Calculator (Is this code ok?)

Posted 14 April 2011 - 12:26 PM

View PostNallo, 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

def add()
    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.
Was This Post Helpful? 0
  • +
  • -

#6 Nallo  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 163
  • View blog
  • Posts: 255
  • 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.
Was This Post Helpful? 0
  • +
  • -

#7 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5907
  • View blog
  • Posts: 12,811
  • 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)

def add():
	(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 add():
	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.
Was This Post Helpful? 1
  • +
  • -

#8 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • 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


Was This Post Helpful? 1
  • +
  • -

#9 Mossypne  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#10 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • 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?
Was This Post Helpful? 0
  • +
  • -

#11 Mossypne  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#12 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • 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

Was This Post Helpful? 0
  • +
  • -

#13 Mossypne  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • 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.

And may you look at my thread about counting a couple threads below this please.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1