Question

In: Computer Science

THIS is to be done in python! Splitting the Bill- An exercise in input validation Write...

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

Solutions

Expert Solution

#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))


Related Solutions

Write a C++ program to do the following USING ARRAYS 1) input 15 integers (input validation...
Write a C++ program to do the following USING ARRAYS 1) input 15 integers (input validation , all numbers must be between 0 and 100) 2) find the largest number 3) Find the Smallest Number 4) Find the Sum of all numbers in the Array Display: as per the user's section (menu using switch or if else ) Largest Number Smallest Number Sum of all numbers the original Array DO NOT USE METHODS OR FUNCTIONS Submit: Source code (C++) output...
Write a Java program to do the following USING ARRAYS 1) input 15 integers (input validation,...
Write a Java program to do the following USING ARRAYS 1) input 15 integers (input validation, all numbers must be between 0 and 100) 2) find the largest number. 3) Find the Smallest Number 4) Find the Sum of all numbers in the Array Display: Largest Number Smallest Number Sum of all numbers the original Array DO NOT USE METHODS OR FUNCTIONS
PYTHON Write a python program that encrypts and decrypts the user input. Note – Your input...
PYTHON Write a python program that encrypts and decrypts the user input. Note – Your input should be only lowercase characters with no spaces. Your program should have a secret distance given by the user that will be used for encryption/decryption. Each character of the user’s input should be offset by the distance value given by the user For Encryption Process: Take the string and reverse the string. Encrypt the reverse string with each character replaced with distance value (x)...
Exercise 2. Automate the boring stuff, page 77, Input Validation. Add try and except statements to...
Exercise 2. Automate the boring stuff, page 77, Input Validation. Add try and except statements to the previous project to detect whether the user types in a noninteger string. Normally, the int() function will raise a ValueError error if it is passed a noninteger string, as in int('puppy'). In the except clause, print a message to the user saying they must enter an integer. THis is the previous project #function definition def collatzSequence(number): # if number % 2 == 0:...
Using Python Question 1 Write an input function. Then use it to supply the input to...
Using Python Question 1 Write an input function. Then use it to supply the input to following additional functions: i) Print multiplication table of the number from 1 to 12. ii) Print the sum of all the numbers from 1 to up the number given. iii) Print if the number supplied is odd or even. Question 2 Write function that asks for input from a user. If the user types 'end', the program exits, otherwise it just keeps going.
In Python Write a function to read a Sudoku board from an input string. The input...
In Python Write a function to read a Sudoku board from an input string. The input string must be exactly 81 characters long (plus the terminating null that marks the end of the string) and contains digits and dots (the `.` character represents an unmarked position). The input contains all 9 rows packed together. For example, a Sudoku board that looks like this: ``` ..7 ... ... 6.4 ... ..3 ... .54 ..2 ... .4. ... 9.. ... ..5 385...
Write a Python class definition called CellPhone to represent a monthly cell phone bill. The bill...
Write a Python class definition called CellPhone to represent a monthly cell phone bill. The bill should contain the following information: customer name (default value is the empty string) account number (default value is 0) number of gigabytes (GB) used over the monthly limit (default value is 0) Include the following methods: a constructor which given a customer name, account number, and number of GB used in excess of the monthly limit, creates a CellPhone object (be sure to account...
Done in C++, Write a program to read the input file, shown below and write out...
Done in C++, Write a program to read the input file, shown below and write out the output file shown below. Use only string objects and string functions to process the data. Do not use c-string functions or stringstream (or istringstream or ostringstream) class objects for your solution. Input File Cincinnati 27, Buffalo 24 Detroit 31, Cleveland 17 Kansas City 24, Oakland 7 Carolina 35, Minnesota 10 Pittsburgh 19, NY Jets 6 Philadelphia 31, Tampa Bay 20 Green Bay 19,...
IN PYTHON Write a program that takes in a positive integer as input, and outputs a...
IN PYTHON Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is: As long as x is greater than 0 Output x % 2 (remainder is either 0 or 1) x = x // 2 Note: The above algorithm outputs the 0's and 1's in reverse order. You will need to write a second function to reverse the string....
Write about strings and input (python). Explain with 2 examples
Write about strings and input (python). Explain with 2 examples
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT