Tuesday, February 05, 2019

Exercises for Programmers: 57 Challenges to Develop Your Coding Skills - Chapter 1 Part b

I don't know why this was so difficult.  I'm pretty sure that /100, *100, /100 is probably wrong anyway, it just works because the math works.  Anyway, rounding UP the pennies in the % because I'm a greedy corporate f*cker.


import math

print("Enter a tip:")
x =input()
print("Tip is: " + str(x))
print("Enter a bill/total: ")
y = input()
print("Bill is: " + str(y))

#this won't work because I need a precision factor (cents) in my ceil()
#so use full ints?
print("Tip is: " + str((y * x/100)*100)) #cents
print("Tip rounded up is: " + str(math.ceil((y * x/100)*100)))  #what do I expect?
print("Total is: " + str(y + (y * x/100)))

#with 3% and 1.85 should be 1.91
print("Total rounded up is: " + str(y + math.ceil((y * x/100)*100)/100))


Output for a good use case:
Enter a tip:
3
Tip is: 3
Enter a bill/total: 
1.85
Bill is: 1.85
Tip is: 5.55
Tip rounded up is: 6.0
Total is: 1.9055
Total rounded up is: 1.91

No comments: