In: Computer Science
THIS is to be done in python!
Splitting the Bill- An exercise in input validation Write a python program called splitBill.py that works as follows:
• (10 points) Allow the user to input a bill amount, a tip percent, and the number of people in their party. o Input validation: Make sure to validate the input data. Allow the user to reenter inputs until they enter values which are valid.
▪ Make it easy on the user: To leave a 20% tip, the user should just enter 20. The tip amount should be a number between 1 and 50.
▪ The bill amount should be some numeric value between 1 and 5000.
▪ The number of people should be between 1 and 20. o Your program must follow the design of the BMI program discussed in lecture.
▪ It must have (a version) of the following functions: getNumber, isInputValid, getValidNumberInput, and main as in BMI. • (5 points) Output the amount in dollars each person in the party should pay, assuming they want to split the cost evenly.
o Round the output to at most 2 decimal places. (Search for python’s round() which is not discussed in the book)
o Put a $ sign in front of the output
. o Create a new function getSplitAmount that serves as the brains of your program and returns the amount each person in the party owes. This function is the brains of this program similar to how the getBMICategory is the brains in the BMI program.
• (10 pts) Testing: Write tester functions for the isInputValid and getSplitAmount functions.
• (3 pts) Comments: Write a comment for the getSplitAmount function Here are some examples of how your program should work when all inputs are valid. The highlighted parts were typed by the user. Bill: 100 Tip percent: 20 Number of people: 6 Each person pays: $20.0 Bill: 27.50 Tip percent: 10 Number of people: 2 Each person pays: $15.12
Here are some examples of how your program should work when all inputs are valid.
The highlighted parts were typed by the user.
Bill: 100
Tip percent: 20
Number of people: 6
Each person pays: $20.0
Bill: 27.50 Tip percent: 10
Number of people: 2
Each person pays: $15.12
#function to get bill as an input
def getBill():
#taking input from user in b
b=float(input("Bill: "))
#return b
return b
#function to check if bill amount is valid or not
def isBillValid(b):
#if b is in betwen 1 and 5000 then it is valid
if(b>=1 and b<=5000):
return True#return True
#otherwise
else:
return False#return False
#function which takes input in b again and again until user enters
a valid bill amount
def getValidBillInput():
#infinite loop
while(1):
#taking input in b
b=float(input("Bill: "))
#if isBillValid function returns true
if(isBillValid(b)):
break#come out of loop
#if isBillValid returns false then loop continues
return b#return b
#function to get tip as an input
def getTip():
#taking input from user in t
t=float(input("Tip percent: "))
#return t
return t
#function to check if tip percent is valid or not
def isTipValid(t):
#if t is in betwen 1 and 50 then it is valid
if(t>=1 and t<=50):
return True#return True
#otherwise
else:
return False#return False
#function which takes input in t again and again until user enters
a valid tip percent
def getValidTipInput():
while(1):
#taking input in t
t=float(input("Tip percent: "))
# if isTipValid function returns true
if(isTipValid(t)):
break#come out of loop
#if isTipValid returns false then loop continues
return t#return t
#function to get number of people as an input
def getNumber():
#taking input from user in n
n=int(input("Number of people: "))
#return n
return n
#function to check if n is valid or not
def isNumberValid(n):
#if n is in betwen 1 and 20 then it is valid
if(n>=1 and n<=20):
return True#return True
#otherwise
else:
return False#return False
#function which takes input in n again and again until user enters
a valid n value
def getValidNumberInput():
while(1):
#taking input in n
n=int(input("Number of people: "))
# if isNumberValid function returns true
if(isNumberValid(n)):
break#come out of loop
#if isNumberValid returns false then loop continues
return n #return n
#function that serves as the brain of the program
def getSplitAmount(bill,tip,people):
#update bill by adding in it the tip(tip percent of the bill)
bill=bill+((bill*tip)/100)
#now return bill/people which means the amount each person in the
party owes
return bill/people
#call getBill and store returned value in bill
bill=getBill()
#if isBillValid returns False
if(isBillValid(bill)==False):
#call getValidBillInput function which will surely return valid
bill amount in bill
bill=getValidBillInput()
#call getTip and store returned value in tip
tip=getTip()
#if isTipValid returns False
if(isTipValid(tip)==False):
#call getValidTipInput function which will surely return valid tip
percent in tip
tip=getValidTipInput()
#call getNumber and store returned value in n
n=getNumber()
#if isNumberValid returns False
if(isNumberValid(n)==False):
#call getValidNumberInput function which will surely return valid
number of people in n
n=getValidNumberInput()
#atlast call getSplitAmount by passing bill,tip and n to it and
print the returned value
#round(number,2) will modify the number such that it will have only
2 digits after decimal point
print("Each person pays:
$",round(getSplitAmount(bill,tip,n),2))