Self taught Python... Rate my calculator! :)

by GBM
7 replies
I'm self teaching myself Python as my first programming language. I tried to scrape together my little bits of Python knowledge to create a basic calculator with addition, subtraction, multiplication, and division. Would anyone mind taking a look at my code and telling me what I've done well, or what could be simpler?

Any help is appreciated

PHP Code:
def addition(num1num2):
    print 
"adding %d and %d..." % (num1num2)
    print 
num1+num2
    
def subtraction
(num1num2):
    print 
"subtracting %d from %d..." % (num2num1)
    print 
num1-num2
    
def multiplication
(num1num2):
    print 
"multiplying %d and %d..." % (num1num2)
    print 
num1*num2
    
def division
(num1num2):
    print 
"%d devided by %d..." % (num1num2)
    print 
num1/num2
    
print "WELCOME TO THE SHIT CALCULATOR!!"
calculation raw_input("What would you like to calculate? \n")

calc_list calculation.split(" ")

if 
"plus" in calc_list:

    
first_num calc_list[0]
    
new_first_num int(first_num)

    
second_num calc_list[2]
    
new_second_num int(second_num)

    
addition(new_first_numnew_second_num)
    
if 
"minus" in calc_list:

    
first_num calc_list[0]
    
new_first_num int(first_num)

    
second_num calc_list[2]
    
new_second_num int(second_num)

    
subtraction(new_first_numnew_second_num)
    
if 
"times" in calc_list:

    
first_num calc_list[0]
    
new_first_num int(first_num)

    
second_num calc_list[2]
    
new_second_num int(second_num)

    
multiplication(new_first_numnew_second_num)
    
if 
"divided" in calc_list:

    
first_num calc_list[0]
    
new_first_num int(first_num)

    
second_num calc_list[3]
    
new_second_num int(second_num)

    
division(new_first_numnew_second_num
#calculator #division #python #rate #taught #working
  • Profile picture of the author Neil Morgan
    Hey GBM,

    Good job - nice clean code. And even though I know nothing about Python, I can see exactly how it works.

    There's not much I can say about this program as it's so simple. If this had been part of a real software solution, which I'm sure you're working up to, I would offer 3 pieces of advice:

    1. When there are lots of "ifs", use a switch / case construct (if that even exists in Python!).

    2. Separate language strings into separate files. When creating solutions for sale, not offering non-English versions cuts out a whole lot of people that might buy your product, not to mention the resale rights you can sell to people whose first language is non English. I know that this doesn't apply here but it's definitely something to consider as you look at ways to monetize your new skill with larger projects.

    3. Comment your code. It can be a pain to do but is worthwhile.

    They're very general points but I hope they help.

    Good luck with your ongoing learning.

    Cheers,

    Neil
    Signature

    Easy email marketing automation without moving your lists.

    {{ DiscussionBoard.errors[7321460].message }}
    • Profile picture of the author GBM
      Originally Posted by Neil Morgan View Post

      Hey GBM,

      Good job - nice clean code. And even though I know nothing about Python, I can see exactly how it works.

      There's not much I can say about this program as it's so simple. If this had been part of a real software solution, which I'm sure you're working up to, I would offer 3 pieces of advice:

      1. When there are lots of "ifs", use a switch / case construct (if that even exists in Python!).

      2. Separate language strings into separate files. When creating solutions for sale, not offering non-English versions cuts out a whole lot of people that might buy your product, not to mention the resale rights you can sell to people whose first language is non English. I know that this doesn't apply here but it's definitely something to consider as you look at ways to monetize your new skill with larger projects.

      3. Comment your code. It can be a pain to do but is worthwhile.

      They're very general points but I hope they help.

      Good luck with your ongoing learning.

      Cheers,

      Neil
      Thanks for the feedback!
      I don't even know what "switch / case construct" is :/
      Looks like I've got to do more research.
      Signature

      _/\_\o/_ JAWS

      {{ DiscussionBoard.errors[7321768].message }}
  • Profile picture of the author gcampton
    Basically if you have a bunch of "ifs" you can do this instead. Lets pretend we are checking what fruit we have.

    Code:
    if (fruit == apple){ }if (fruit == orange){etc etc... there's a better way }
    
    switch (fruit)
    {
        case "apple":  
                 then eat the apple;  break;
        case "orange":
                 then eat the orange;  break;
        case "pineapple":
                 then eat the pineapple;  break;
        case "watermelon":
                 then throw it at the nearest grandmother;   break;
    
        case default:
                 then eat some pizza;  break;
    }
    Signature

    ()_()
    (o.O) <<<----- This bunny is more ethical and mostly made of pixels.
    (")("")

    {{ DiscussionBoard.errors[7321813].message }}
  • Profile picture of the author locke815
    Great coding there.
    {{ DiscussionBoard.errors[7324751].message }}
  • Profile picture of the author wayfarer
    Yeah, there's no switch construct in Python. Keep studying Python and just ignore the switch advice

    If you want a cleaner if/elif construct, you could try assigning function names to a dictionary and calling it by key name...
    Signature
    I build web things, server things. I help build the startup Veenome. | Remote Programming Jobs
    {{ DiscussionBoard.errors[7326862].message }}
  • Profile picture of the author Ttrain
    Python really does make php look ugly! But PHP is still my "get shit done" language and I'm sticking with it for now.
    And from what I've heard python doesn't have a switch statement (I could be wrong). But your code does look really clean, nice work.
    {{ DiscussionBoard.errors[7332974].message }}

Trending Topics