Post Reply 
Python Coding
05-16-2022, 12:06 PM
Post: #1
Python Coding
Been a long time, anyway while learning on how to code using the

Python Programming Language and my first program is of cause the

good old "Profit on Pricing" calculations. This program I use the same

algorithm on my several HP programmable calculator found elsewhere here.

Here I'll share my code and this might be useful to some who also learn coding.

This link user can run this program and try out.

Enjoy !!

Link: https://trinket.io/python/656c020f51?runOption=run

Gamo 5/2022
Find all posts by this user
Quote this message in a reply
05-17-2022, 12:36 AM
Post: #2
RE: Python Coding
You can use this Python to RPN - source code converter to translate your Python code for the HP-42S.

However, I had to adjust it a little bit to make it work:
Code:
LBL("Profit on Pricing")

p = 100 # Constant use for percentage

CST = PROMPT("Enter Cost  : ")
SEL = PROMPT("Enter Price : ")
MUP = PROMPT("Enter Markup: ")
MAR = PROMPT("Enter Margin: ")

if CST and SEL != 0: # if both true
   MAR = p * (SEL - CST) / SEL
   MUP = p * (SEL - CST) / CST

if MAR == 0: # if true compute Margin
   MAR = MUP / (1 + MUP / p)

# constant to plug into remaining formulas
q = 1 - MAR / p

if MUP == 0: # if true compute Markup
   MUP = MAR / q

if CST == 0: # if true compute Cost
   CST = SEL * q

if SEL == 0: # if true compute Price
   SEL = CST / q

print("==================")        
print("Cost   = " "%1.2f"%(CST))
print("Price  = " "%1.2f"%(SEL))
print("Markup = " "%1.1f"%(MUP)), ("%")
print("Margin = " "%1.1f"%(MAR)), ("%")
print("==================")
print("Profit = " "%1.2f"%(SEL - CST))
print("==========================================")

Some of the generated code can be simplified a bit.
For instance the following snippet:
Code:
if MAR == 0: # if true compute Margin
   MAR = MUP / (1 + MUP / p)

… is translated to:
Code:
RCL "MAR"       // MAR
0
XEQ 72          // compare, return bool
X≠0?            // if true?
GTO 02          // if body
GTO 03          // resume
LBL 02          // if body
RCL "MUP"       // MUP
1
RCL "MUP"       // MUP
RCL 00          // p
/
+
/
STO "MAR"       // MAR 
LBL 03          // resume

But we can use instead:
Code:
RCL "MAR"       // MAR
X≠0?            // if set?
GTO 03          // resume
RCL "MUP"       // MUP
1
RCL "MUP"       // MUP
RCL 00          // p
/
+
/
STO "MAR"       // MAR 
LBL 03          // resume

However the generated code to print the results doesn't appear to work.

Still I hope that you have fun with Python and your programs!
Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 1 Guest(s)