~sporiff/soap-calculator

ref: ae2bca34dd2dbae7db4f31d7e45e44b8597b5df9 soap-calculator/soap_calculator.py -rw-r--r-- 2.7 KiB
ae2bca34 — Ciarán Ainsworth Corrected percentages, improved feedback 6 months ago
                                                                                
a9791766 Ciarán Ainsworth
ae2bca34 Ciarán Ainsworth
a9791766 Ciarán Ainsworth
ae2bca34 Ciarán Ainsworth
a9791766 Ciarán Ainsworth
078a8cd6 Ciarán Ainsworth
a9791766 Ciarán Ainsworth
ae2bca34 Ciarán Ainsworth
a9791766 Ciarán Ainsworth
ae2bca34 Ciarán Ainsworth
a9791766 Ciarán Ainsworth
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
# Store saponification amounts for later

coconut_lye = 0.1910
olive_lye = 0.1353

# Make a function to ensure that measurements are entered as numbers, not strings

def input_measurements(question):
    while True:
        try:
            userInput = float(input(question))
        except ValueError:
            print("Please input a number\n")
            continue
        else:
            return userInput
        break

# Make a function to ensure that we get a proper yes/no answer to our question (accept variations on these answers)
    
def yes_no(question):
    yes = set(['yes','y', 'ye', 'yep'])
    no = set(['no','n', 'nope'])
     
    while True:
        choice = input(question).lower()
        if choice in yes:
           return True
        elif choice in no:
           return False
        else:
            print("Please respond with 'yes' or 'no'\n")

# Make a function to ensure we get a number between 0-10

def percentage(question):
    while True:
        userInput = input_measurements(question)
        if userInput < 0 or userInput > 10:
            print("Please input a number between 0 and 10\n")
            continue
        else:
            return userInput
        break
    
print("Welcome to the soap calculator. Please follow the instructions onscreen to get started.\n")

# Get olive oil measurements and store this as a variable

olive_oil = input_measurements("Please enter the amount of olive oil you would like to use in grammes.\n")

print("Okay, {O}g of Olive Oil.\n".format(O = olive_oil))

# Get coconut oil measurements and store this as a variable

coconut_oil = input_measurements("Please enter the amount of coconut oil you would like to use in grammes.\n")

print("Okay, {C}g of Coconut Oil.\n".format(C = coconut_oil))

# Check if the user wants to reduce their lye content

use_reduction = yes_no("Would you like to use lye reduction?\n")

# If the user chooses to reduce their content, get a percentage between 0 - 10

if use_reduction:
    lye_reduction = percentage("Please enter the percentage of reduction you would like to use (between 0 and 10)\n")
    print("Using {P}% lye reduction.\n".format(P = lye_reduction))

# Calculate the total values and return them to the user
    
water_needed = (coconut_oil + olive_oil) / 3
if use_reduction:
    coconut_lye = coconut_lye - ((lye_reduction/100) * coconut_lye)
    olive_lye = olive_lye - ((lye_reduction/100) * olive_lye)

coconut_lye_needed = coconut_oil * coconut_lye
olive_lye_needed = olive_oil * olive_lye
    
lye_needed = coconut_lye_needed + olive_lye_needed

print("For {O}g of Olive Oil and {C}g of Coconut Oil, you will need:\n* {W:.3f}ml of water\n* {L:.3f}g of lye".format(O = olive_oil, C = coconut_oil, W = water_needed, L = lye_needed))