## Wednesday, February 06, 2019

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

Version 3, or c, depending on your preference. Goal is to only enter numbers. This wasn't working for me very well until I swapped input for raw_input. Apparently I could have eval-ed the input, or been a normal boy and just used Python 3. It's on here somewhere.

```import math

#Version 1: basic
#Version 2: round up
#Version 3: next, ONLY enter numbers

def inputNumber(message):
while True:
try:
return float(raw_input(message))
except ValueError:
return inputNumber("Not an integer! 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
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))

```
This was actually way more interesting on output then I'd have expected. First, I was using a function from 101 computing, and they converted input to int...that was no good. It rounded my whole bill. So I used a float. Which works. Even with a more complicated set of inputs. And then my non-numeric inputs crashed, which is where I had to switch to raw_input for 2.7 and a tighter loop.
```Enter a tip:7
Tip is: 7
Enter a bill/total: 2.50
Bill is: 2
Tip is: 0
Tip rounded up is: 0.0
Total is: 2
Total rounded up is: 2.0
>>> ================================ RESTART ================================
>>>
Enter a tip:7
Tip is: 7.0
Enter a bill/total: 2.50
Bill is: 2.5
Tip is: 17.5
Tip rounded up is: 18.0
Total is: 2.675
Total rounded up is: 2.68
>>> ================================ RESTART ================================
>>>
Enter a tip:8.5
Tip is: 8.5
Enter a bill/total: 2.57
Bill is: 2.57
Tip is: 21.845
Tip rounded up is: 22.0
Total is: 2.78845
Total rounded up is: 2.79
>>>
```