Thursday, February 07, 2019

Exercises for Programmers: 57 Challenges to Develop Your Coding Skills - Chapter 1 Part d/e

It was exciting to find out I'd already done part d/4, so I jumped right into no negative numbers. That would be a really mean tipping system. I found some other bugs as I went - I spelled negative wrong, the tip info didn't say "cents" so you couldn't be sure what was going on there, and my error handling message wasn't generic enough to cover all cases (versus specific messages, which is annoying for me, although good for the user).
import math

#Version 1: basic
#Version 2: round up
#Version 3: next, ONLY enter numbers
#Version 4: ask again for a number - already done!
#Version 5: no negative numbers

def inputNumber(message):
  while True:
    try:
       returnValue = float(raw_input(message))
       if returnValue < 0:
           raise ValueError
       else:
           return returnValue
    except ValueError:
       return inputNumber("Not a non-negative value! Try again.")


#print("Enter a tip:")
x = inputNumber("Enter a tip:")
print("Tip is: " + str(x))
#print("Enter a bill/total: ")
y = inputNumber("Enter a bill/total: ")
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") #cents
print("Tip rounded up is: " + str(math.ceil((y * x/100)*100)) + " cents")  #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))

I notice that I don't always have the right precision...my money isn't necessarily two decimal spots. I think I know how to fix that with a decimal or I could bite the bullet and use one of the money packages/imports that exists. That might be part 8. I notice I have to flip it to a GUI coming up here soon...I'll have to port this code over to javascript (although I'd be much, much faster with C#).

Enter a tip:-1
Tip is: -1.0
Enter a bill/total: 1.50
Bill is: 1.5
Tip is: -1.5
Tip rounded up is: -1.0
Total is: 1.485
Total rounded up is: 1.49
>>> ================================ RESTART ================================
>>> 
Enter a tip:-1
Not an integer! Try again.-2
Not an integer! Try again.3
Not an integer! Try again.4
Not an integer! Try again.
>>> ================================ RESTART ================================
>>> 
Enter a tip:-2
Not an integer! Try again.3
Tip is: 3.0
Enter a bill/total: 150
Bill is: 150.0
Tip is: 450.0
Tip rounded up is: 450.0
Total is: 154.5
Total rounded up is: 154.5
>>> ================================ RESTART ================================
>>> 
Enter a tip:-3
Not a non-negiative value! Try again.0
Tip is: 0.0
Enter a bill/total: 150
Bill is: 150.0
Tip is: 0.0 cents
Tip rounded up is: 0.0
Total is: 150.0
Total rounded up is: 150.0
>>> ================================ RESTART ================================
>>> 
Enter a tip:-2
Not a non-negative value! Try again.-3
Not a non-negative value! Try again.-4
Not a non-negative value! Try again.3
Tip is: 3.0
Enter a bill/total: 17.25
Bill is: 17.25
Tip is: 51.75 cents
Tip rounded up is: 52.0
Total is: 17.7675
Total rounded up is: 17.77
>>> 

No comments: