forked from DevPops-Inc/python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalculateTotalProfitInPython.py
More file actions
127 lines (87 loc) · 4.16 KB
/
calculateTotalProfitInPython.py
File metadata and controls
127 lines (87 loc) · 4.16 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/bin/python
# calculate total profit in Python
# you can run this script with: python3 calculateTotalProfitInPython.py < total sale > < profit margin >
import colorama, os, sys, traceback
from colorama import Fore, Style
from datetime import datetime
colorama.init()
def checkOs():
print("Started checking operating system at", datetime.now().strftime("%m-%d-%Y %I:%M %p"))
if sys.platform == "win32":
print(Fore.GREEN + "Operating System:", end=""); sys.stdout.flush()
os.system('ver')
print(Style.RESET_ALL, end="")
operatingSystem = "Windows"
elif sys.platform == "darwin":
print(Fore.GREEN + "Operating System: ")
os.system('sw_vers')
print(Style.RESET_ALL, end="")
operatingSystem = "macOS"
elif sys.platform == "linux":
print(Fore.GREEN + "Operating System: ")
os.system('uname -r')
print(Style.RESET_ALL, end="")
operatingSystem = "Linux"
print("Finished checking operating system at", datetime.now().strftime("%m-%d-%Y %I:%M %p"))
print("")
return operatingSystem
def getTotalSale(operatingSystem):
if operatingSystem == "Windows":
totalSale = float(input("Please type the total sale amount and press \"Enter\" key (Example: 100): "))
elif operatingSystem == "macOS" or operatingSystem == "Linux":
totalSale = float(input("Please type the total sale amount and press \"return\" key (Example: 100): "))
print("")
return totalSale
def getProfitMargin(operatingSystem):
if operatingSystem == "Windows":
profitMargin = float(input("Please type the profit margin and press \"Enter\" key (Example: 20): "))
if operatingSystem == "macOS" or operatingSystem == "Linux":
profitMargin = float(input("Please type the profit margin and press \"Enter\" key (Example: 20): "))
print("")
return profitMargin
def checkParameters(totalSale, profitMargin):
print("Started checking parameter(s) at", datetime.now().strftime("%m-%d-%Y %I:%M %p"))
valid = True
print("Parameter(s):")
print("--------------------------------------")
print("totalSale : {0}".format(totalSale))
print("profitMargin: {0}".format(profitMargin))
print("--------------------------------------")
if totalSale == None or totalSale == "":
print(Fore.RED + "totalSale is not set." + Style.RESET_ALL)
valid = False
if profitMargin == None or profitMargin == "":
print(Fore.RED + "profitMargin is not set." + Style.RESET_ALL)
valid = False
if valid == True:
print(Fore.GREEN + "All parameter check(s) passed." + Style.RESET_ALL)
print("Finished checking parameter(s) at", datetime.now().strftime("%m-%d-%Y %I:%M %p"))
print("")
else:
raise Exception("One or more parameter check(s) passed.")
def calculateTotalProfit():
print("\nCalculate total profit in Python.\n")
operatingSystem = checkOs()
if len(sys.argv) > 2:
totalSale = float(sys.argv[1])
profitMargin = float(sys.argv[2])
else:
totalSale = getTotalSale(operatingSystem)
profitMargin = getProfitMargin(operatingSystem)
try:
checkParameters(totalSale, profitMargin)
startDateTime = datetime.now()
print("Started calculating total profit at", startDateTime.strftime("%m-%d-%Y %I:%M %p"))
totalProfit = totalSale * (profitMargin/100)
print(Fore.BLUE + "Total profit from total sale of ${0} with {1}% profit margin is ${2}".format(totalSale, profitMargin, totalProfit) + Style.RESET_ALL)
print(Fore.GREEN + "Successfully calculated total profit." + Style.RESET_ALL)
finishedDateTime = datetime.now()
print("Finished calculated total profit at", finishedDateTime.strftime("%m-%d-%Y %I:%M %p"))
duration = finishedDateTime - startDateTime
print("Total execution time: {0} second(s)".format(duration.seconds))
print("")
except Exception as e:
print(Fore.RED + "Failed to calculate total profit in Python.")
traceback.print_exc()
exit("" + Style.RESET_ALL)
calculateTotalProfit()