[Python] Small Python program applying Skeleton

This is an example of how to apply skeleton in the previous post.
This program is just the simplest calculator in the world. The purpose is just to prove how efficient to apply the skeleton into your project. It makes the project have a good look and well-organized.

--------------------
Chương trình máy tính python đơn giản. Cách áp dụng coding style ở bài trước
--------------------



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
"""
--------------------------------------------------
 mathhoang (mathhoang.blogspot.com)
--------------------------------------------------
    Even this small segment of code does not cover all of the cases, it's still good
    for beginner to start learning Python
    There are still some cases not been covered in this small program, you still
    have room to impove it. This is left for you as exercises.
    
    Improvements needed:+ adding more operation
                        + check the case when users input some special keys such as: Enter,
                         space,....
                        + optimize the code to make it shorter and more readable
                        + modify to make it have a friendly interaction with user
                        + improve code structure
                        + etc.....
"""

import sys

# print the calculation result
def print_result(op1,op2,result,operator):
    print("{0} {2} {1} = {3}".format(op1,op2,operator,result))

# display the guildine
def print_guide():
    print("1: addition")
    print("2: subtraction")
    print("3: multiplication")
    print("4: division")
    print("5: power")
    print("6: guidline")
    print("7: exit program")

# display the message in case users input the invalid key
def print_no_operation():
    print("You have entered incorrect operator");
    print("Please press 6 for help")

# main control flow    
def main():
    
    print_guide()

    while(1):
        print("---------------------------------------")
        operator = int(input("Enter operation: "))
        
        if(int(operator) >= 1 and int(operator) <= 5):
            """ get the input """
            op1 = int(input("Enter the 1st operand: "))
            op2 = int(input("Enter the 2nd operand: "))

            """Just to make the interpreter happy"""
            result = "undefined"
            str_op = "undefined"
            
            if(operator == 1):
                result = op1 + op2
                str_op = '+'    #addition
            elif(operator == 2):
                result = op1 - op2
                str_op = '-'    #subtraction
            elif(operator == 3):
                result = op1 * op2
                str_op = '*'    #multiplication
            elif(operator == 4):
                result = op1/op2
                str_op = '/'    #division
            elif(operator == 5):
                result = op1**op2
                str_op = '^'    #exponent                
            """Pint out the result"""
            print_result(op1,op2,result,str_op)
        elif(operator == 6):
            print_guide()   #Print out the guidline
        elif(operator == 7):
            print("you are about existing the program! Thanks for giving a try!!")
            break; #Exit program
        else:
            print_no_operation(); #invalid operation

# call the main function    
if __name__ == '__main__':
    sys.exit(main())

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...