Question

In: Computer Science

Write the following easy Python functions: 1) Write the function named roundDollars(). The function has one...

Write the following easy Python functions:

1) Write the function named roundDollars(). The function has one input, a String named amountStr which consists of a dollar-formatted amount, such as "$ 1,256.86". The returned value is an int representing the number of rounded "dollars" in the amount, (1257 in the sample shown here). You will need to scrub, format and parse the input, then use arithmetic to determine how many rounded dollars the amount contains.

roundDollars("$ 1,256.86") → 1257

roundDollars("$ 0.42") → 0

roundDollars("$375.00") → 3752

2) Write the function named lineItem() that formats an item on a receipt. The function has two parameters, a String description and a double price. Return a formatted string where description is left-aligned in a column 15 wide and the price is right-aligned in a column 10 wide with two decimals. Thousands should be separated by commas but there should be no dollar sign. For example:

lineItem("Toaster", 24) → "Toaster 24.00"

lineItem("Orange Juice", 3.59) → "Orange Juice 3.59"

lineItem("Carbon Fiber Bike", 1129) → "Carbon Fiber Bi 1,129.00"

3) Write the function named change() that returns a list of coins, formatted in String form, returned from a purchase. The function has two parameters, the amountTendered and the purchasePrice, both doubles. Your function calculates the change due, and then formats the number of dollars, quarters (.25), dimes (.10), nickels (.05) and pennies (.01) due. Each number will be an int and it will be formatted in a column 5 wide. (In other words, your output will have exactly 5 columns and your returned String will be 25 characters long.

change(20, 3.89) → ' 16 0 1 0 1'

change(100, 38.12) → ' 61 3 1 0 3'

change(.25,.20) → ' 0 0 0 1 0'

Solutions

Expert Solution

1. ROUND DOLLARS

Please refer to the following code for the solution

# declaring Function
def roundDollars(amountStr):
    # Removing , from the amount str
    amountStr = amountStr.replace(',', '')
    # Fetching string after $
    amountStr = float(amountStr[1:])
    # Rounding Off
    return round(amountStr)


# Driver code to run Program
print(roundDollars('$1,256.86'))
print(roundDollars("$ 0.42"))
print(roundDollars("$375.00"))

Output:-

1257
0
375

===============================================================================

Refer to the images for better understanding

======================================================

2. Line Item

Please refer to the following code

# declaring Function
def lineItem(description, price):
    # conversion to float and comma seperation
    price="{0:,.2f}".format(price)
    # returning in desired
    return description +" " + price


# Driver code to run Program
print(lineItem('Toaster' ,24))
print(lineItem("Orange Juice", 3.59))
print(lineItem("Carbon Fiber Bike", 1129))

Output:-

Toaster 24.00
Orange Juice 3.59
Carbon Fiber Bike 1,129.00

============================================================

Please refer to the following images for better understanding:-

=========================================================

3. Change

Please refer to the following code:-

# declaring Function
def change(amountTendered, purchasedPrice):
    amount_to_be_returned = amountTendered-purchasedPrice
    # dollor find
    dollor = int(amount_to_be_returned)
    left = amount_to_be_returned - dollor
    left = float("{0:.2f}".format(left))
    # quater find
    quaters = int(left / 0.25)
    left = left - 0.25 *quaters
    # conversion to precision
    left = float("{0:.2f}".format(left))
    # dimes find
    dimes = int(float(left) / 0.10)
    left = left - 0.10 * dimes
    left = float("{0:.2f}".format(left))
    # nickels find
    nickels = int(float(left) / 0.05)
    left = left - nickels * 0.05
    left = float("{0:.2f}".format(left))
    # pennies find
    pennies= int(float(left) /.01)
    # desired output
    return str(dollor)+' ' +str(quaters)+ ' ' +str(dimes)+' '+str(nickels)+ ' '+ str(pennies)

# Driver code to run Program
print(change(20, 3.89))
print(change(100, 38.12))
print(change(.25,.20))

Output:-

16 0 1 0 1
61 3 1 0 3
0 0 0 1 0

=============================================

Please refer to the following images for better understanding


Related Solutions

Write a Python module that must satisfy the following- Define a function named rinsert. This function...
Write a Python module that must satisfy the following- Define a function named rinsert. This function will accept two arguments, the first a list of items to be sorted and the second an integer value in the range 0 to the length of the list, minus 1. This function shall insert the element corresponding to the second parameter into the presumably sorted list from position 0 to one less than the second parameter’s index.   Define a function named rinsort. This...
Use Python for this quetions: Write a python functions that use Dictionary to: 1) function name...
Use Python for this quetions: Write a python functions that use Dictionary to: 1) function name it addToDictionary(s,r) that take a string and add it to a dictionary if the string exist increment its frequenc 2) function named freq(s,r) that take a string and a record if the string not exist in the dictinary it return 0 if it exist it should return its frequancy.
Using python Write a program that has 3 functions, named write_to_file, read_from_file, and ask_user. The write_to_file...
Using python Write a program that has 3 functions, named write_to_file, read_from_file, and ask_user. The write_to_file function should have 2 parameters, file_name and data. When called, the function will open a file with the name stored in the file_name variable, write the information stored in data, then close the file. The read_from_file function will have 1 parameter, file_name. When called, the function will open a file with the name stored in the file_name variable, print the contents of the file,...
Language Python with functions and one main function Write a program that converts a color image...
Language Python with functions and one main function Write a program that converts a color image to grayscale. The user supplies the name of a file containing a GIF or PPM image, and the program loads the image and displays the file. At the click of the mouse, the program converts the image to grayscale. The user is then prompted for a file name to store the grayscale image in.
Program in Python Problem Statement Write a program with the following functions:  wordCount. This function...
Program in Python Problem Statement Write a program with the following functions:  wordCount. This function should accept a string as a parameter and return the number of words contained in the string.  mostFrequentWord. This function accepts a string as a parameter and returns the word that occurs the most frequently in the string.  replaceWord. This function accepts three strings as parameters, let’s call them string1, string2, and string3. It searches string1 for all occurrences of string2. When...
Write functions that do the following in Python: i) A function that takes 2 arguments and...
Write functions that do the following in Python: i) A function that takes 2 arguments and adds them. The result returned is the sum of the parameters. ii) A function that takes 2 arguments and returns the difference, iii) A function that calls both functions in i) and ii) and prints the product of the values returned by both.
Python Problem 3 Write a function named enterNewPassword. This function takes no parameters. It prompts the...
Python Problem 3 Write a function named enterNewPassword. This function takes no parameters. It prompts the user to enter a password until the entered password has 8-15 characters, including at least one digit. Tell the user whenever a password fails one or both of these tests.
Write functions in Python IDLE that do the following: i) A function that takes 2 arguments...
Write functions in Python IDLE that do the following: i) A function that takes 2 arguments and adds them. The result returned is the sum of the parameters. ii) A function that takes 2 arguments and returns the difference, iii) A function that calls both functions in i) and ii) and prints the product of the values returned by both.
Write code to define a function named mymath. The function has three arguments in the following...
Write code to define a function named mymath. The function has three arguments in the following order: Boolean, Integer, and Integer. It returns an Integer. The function will return a value as follows: 1. If the Boolean variable is True, the function returns the sum of the two integers. 2. If the Boolean is False, then the function returns the value of the first integer - the value of the second Integer.
solve with python 3.8 please 1,Write a function called make_squares_coordinate_pair that has one parameter - an...
solve with python 3.8 please 1,Write a function called make_squares_coordinate_pair that has one parameter - an int. The function should return a tuple containing that int and its square. Suppose you were to call your function like this: print(make_squares_coordinate_pair(5)) This should output: (5, 25) Your function MUST be called make_squares_coordinate_pair. You can write code in the main part of your program to test your function, but when you submit, your code must ONLY have the function definition! 2, Write a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT