This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## 1. Write a program that takes the radius of a sphere (a floating-point number) as input and outputs the sphere’s diameter, circumference, surface area, and volume. | |
def circle_calc(): | |
input_radius = float(input("Please enter the radius: ")) | |
#Diameter is always twice the size of radius | |
diameter = 2 * input_radius | |
print("Diameter of the cirlce is :",round(diameter,2)) | |
#Circumference forumula |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## 2. An employee’s total weekly pay equals the hourly wage multiplied by the total number of regular hours plus any overtime pay. Overtime pay equals the total overtime hours multiplied by 1.5 times the hourly wage. Write a program that takes as inputs the hourly wage, total regular hours, and total overtime hours and displays an employee’s total weekly pay. | |
def employee_wages(): | |
print("***Employee Wages***") | |
print("Please enter the below hours worked and bill rate of an employee") | |
hourly_wage = float(input("Hourly wage: ")) | |
total_regular_hrs = float(input("Regular hours: ")) | |
total_overtime_hrs = float(input("Overtime hours: ")) | |
total_wages = hourly_wage * total_regular_hrs + (hourly_wage * 1.5) * total_overtime_hrs | |
print("Total wages:", total_wages) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## 3. A standard science experiment is to drop a ball and see how high it bounces. Once the “bounciness” of the ball has been determined, the ratio gives a bounciness index. For example, if a ball dropped from a height of 10 feet bounces 6 feet high, the index is 0.6 and the total distance traveled by the ball is 16 feet after one bounce. If the ball were to continue bouncing, the distance after two bounces would be 10 ft + 6 ft + 6 ft + 3.6 ft = 25.6 ft. Note that distance traveled for each successive bounce is the distance to the floor plus 0.6 of that distance as the ball comes back up. Write a program that lets the user enter the initial height of the ball and the number of times the ball is allowed to continue bouncing. Output should be the total distance traveled by the ball | |
def bounciness(): | |
print("***Ball bounciness***") | |
print("Please enter the details of the ball initial height thrown and no of bounces") | |
initial_height = float(input("Initial height: ")) | |
no_of_times_bounces = int(inpu |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## 3. The German mathematician Gottfried Leibniz developed the following method to approximate the value of π: π/4 = 1 – 1/3 + 1/5 – 1/7 + ... Write a program that allows the user to specify the number of iterations used in this approximation and displays the resulting value. | |
def calculate_pi_value(): | |
print("***Pi value Calculation***") | |
iterations_count = int(input("Iterations count: ")) | |
index = 0 | |
for i in range(1, iterations_count + 1, 2): | |
if i % 4 == 1: | |
index += 1/i |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## 5. The TidBit Computer Store has a credit plan for computer purchases. There is a 10% down payment and an annual interest rate of 12%. Monthly payments are 5% of the listed purchase price minus the down payment. Write a program that takes the purchase price as input. The program should display a table, with appropriate headers, of a payment schedule for the lifetime of the loan. Each row of the table should contain the following items: n The month number (beginning with 1) n The current total balance owed n The interest owed for that month n The amount of principal owed for that month n The payment for that month n The balance remaining after payment The amount of interest for a month is equal to balance * rate / 12. The amount of principal for a month is equal to the monthly payment minus the interest owed. | |
def calc_emi(): | |
print("***TidBit Computer Store has a credit plan for computer purchases***") | |
purchase_price = float(input("Enter the purchase price: ")) | |
down_payment = purchase_price * 0.1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## 6. The Payroll Department keeps a list of employee information for each pay period in a text file. The format of each line of the file is <last name> <hourly wage> <hours worked> Write a program that inputs a filename from the user and prints a report to the terminal of the wages paid to the employees for the given period. The report should be in tabular format with the appropriate header. Each line should contain an employee’s name, the hours worked, and the wages paid for that period. | |
def print_payroll_report(filename): | |
try: | |
with open(filename, 'r') as file: | |
print("***Payroll Report Summary***") | |
print("{:<15} {:<12} {:<10}".format("Employee Name", "Hours Worked", "Wages Paid")) | |
print("-" * 40) | |
for line in file: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## 7. Statisticians would like to have a set of functions to compute the median and mode of a list of numbers. The median is the number that would appear at the midpoint of a list if it were sorted. The mode is the number that appears most frequently in the list. Define these functions in a module named stats.py. Also include a function named mean, which computes the average of a set of numbers. Each function expects a list of numbers as an argument and returns a single number. | |
def median_calc(numbers_list): | |
sorted_numbers = sorted(numbers_list) | |
n = len(sorted_numbers) | |
if n % 2 == 0: | |
mid = n // 2 | |
return (sorted_numbers[mid - 1] + sorted_numbers[mid]) / 2 | |
else: | |
mid = n // 2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#8. Write a program that allows the user to navigate through the lines of text in a file. The program prompts the user for a filename and inputs the lines of text into a list. The program then enters a loop in which it prints the number of lines in the file and prompts the user for a line number. Actual line numbers range from 1 to the number of lines in the file. If the input is 0, the program quits. Otherwise, the program prints the line associated with that number. | |
def file_read(filename): | |
try: | |
with open(filename, 'r') as file: | |
lines = file.readlines() | |
return lines | |
except FileNotFoundError: | |
print("File not found.") | |
return [] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# A simple software system for a library models a library as a collection of books and patrons. A patron can have at most three books out on loan at any given time. A book also has a list of patrons waiting to borrow it. Each book has a title, an author, a patron to whom it has been checked out, and a list of patrons waiting for that book to be returned. Each patron has a name and the number of books it has currently checked out. Develop the classes Book and Patron to model these objects. Think first of the interface or set of methods used with each class, and then choose appropriate data structures for the state of the objects. Also, write a short script to test these classes. | |
class Patron: | |
def __init__(self, name): | |
self.name = name | |
self.checked_out_books = [] | |
def checkout_book(self, book): | |
if len(self.checked_out_books) < 3: | |
self.checked_out_books.append(book) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 10 Develop a Library class that can manage the books and patrons from Project 9. This class should include methods for adding, removing, and finding books and patrons. There should also be methods for borrowing and returning a book. Write a script to test all these methods. | |
class Patron: | |
def __init__(self, name): | |
self.name = name | |
self.checked_out_books = [] | |
def checkout_book(self, book): | |
if len(self.checked_out_books) < 3: | |
self.checked_out_books.append(book) |
OlderNewer