Skip to content

Instantly share code, notes, and snippets.

@camckin10
Last active April 1, 2019 16:25
Show Gist options
  • Save camckin10/07b1dff79250fc57ca4b435282bc2b09 to your computer and use it in GitHub Desktop.
Save camckin10/07b1dff79250fc57ca4b435282bc2b09 to your computer and use it in GitHub Desktop.
Programming Exercises--to add to repo
QUESTION #6--
Create a program that determines how many years you have left until retirement and the year you can retire. It should prompt for your current age and the age you want to retire and display the output as shown in the example that follows.
Example Output
What is your current age? 25
At what age would you like to retire? 65
You have 40 years left until you can retire.
It's 2015, so you can retire in 2055.
Constraints
• Again, be sure to convert the input to numerical data before doing any math.
• Don’t hard-code the current year into your program. Get it from the system time via your programming lan- guage.
SOLUTION USING PYTHON3
import datetime
a = input('What is your current age?')
b = input('At what age would you like to retire?')
c = int(b) - int(a)
print("You have"+ " " + str(c) + " " + "years until you can retire.")
currYear = datetime.datetime.now()
d = currYear.strftime("%Y")
e = int(d) + int(c)
print("Its"+ " "+ str(d) + " " + "so you can retire in" + " " + str(e))
----------------------------------------------------------------------------------------------------------
QUESTION #7--Create a program that calculates the area of a room. Prompt the user for the length and width of the room in feet. Then display the area in both square feet and square meters.
Example Output
What is the length of the room in feet? 15
What is the width of the room in feet? 20
You entered dimensions of 15 feet by 20 feet.
The area is
300 square feet
27.871 square meters
The formula for this conversion is
m2 = f2 × 0.09290304
Constraints
• Keep the calculations separate from the output. • Use a constant to hold the conversion factor.
SOLUTION using python3
l = input('What is the length in feet?')
w = input('What is the width in feet?')
result = int(l) * int(w)
print(result)
print("You entered dimensions of" + " " + str(l) + " " + "feet"
+ " " + "by" + " "+ str(w)+ " " + "feet")
m = result ** 2
print(m)
print("The area is" + " "+ str(result) + " "+ "square feet" + str(m) + "square meters.")
Should print:
What is the length in feet? 8
What is the width in feet? 9
72
You entered dimensions of 8 feet by 9 feet
5184
The area is 72 square feet5184square meters.
------------------------------------------------------------------------------------
QUESTION #8 --Pizza Party
Write a program to evenly divide pizzas. Prompt for the number of people, the number of pizzas, and the number of slices per pizza. Ensure that the number of pieces comes out even. Display the number of pieces of pizza each person should get. If there are leftovers, show the number of leftover pieces.
Example Output
How many people? 8
How many pizzas do you have? 2
8 people with 2 pizzas
Each person gets 2 pieces of pizza.
There are 0 leftover pieces.
Challenge -- Create a variant of the program that prompts for the number of people and the number of pieces each person wants, and calculate how many full pizzas you need to purchase.
SOLUTION(using Python3)
a = input("How many people?")
b = input("How many pizzas do you have?")
rem_pieces = int(a) / int(b)
print(str(a) + " " + "people with" + " " + str(b) + " " + "pizzas.")
print("Each person gets" + " " + str(b) + " " + "pieces of pizza.")
print("There are" + " " + str(rem_pieces) + " " + "leftover pieces.")
CHALLENGE SOLUTION
num_people = input("How many people?")
num_pieces = input("How many pieces does each person want?")
num_pizzas = int(num_people) * int(num_pieces)
print(num_pizzas)
-------------------------------------------------------------------------------------
QUESTION #9
Paint Calculator--Calculate gallons of paint needed to paint the ceiling of a room. Prompt for legnth and width , and assume one gallong covers
350 sq. feet. Display the number of gallons needed to paint the ceiling as a whole numbers
EXAMPLE OUTPUT
You will need to purchase 2 gallons of paint to cover 360 sq. feet.
CONSTRAINTS
*Use a constant to hold the coversion rate.
*Ensure you round up to the next number.
#will need to use math.ceil module to convert floating numbers into whole numbers
import math
length= input("What is the length?")
width = input("What is the width?")
area = int(length) * int(width)
gallon = 350
print(area) #printing area
result = area/gallon
print(math.ceil(result))
print("You will need to purchase" + " " + str(math.ceil(result)) + " " + "gallons of paint to cover" +
" " + str(area) + " " + "sq. feet.")
------------------------------------------------------------------------------------------------------------
QUESTION #10
Create a simple self-checkout system. Prompt for the prices and quantities of three items. Calculate the subtotal of the items. Then calculate the tax using a tax rate of 5.5%. Print out the line items with the quantity and total, and then print out the subtotal, tax amount, and total.
Example Output
Enter the price of item 1: 25
Enter the quantity of item 1: 2
Enter the price of item 2: 10
Enter the quantity of item 2: 1
Enter the price of item 3: 4
Enter the quantity of item 3: 1
Subtotal: $64.00
Tax: $3.52
Total: $67.52
Constraints
• Keep the input, processing, and output parts of your program separate. Collect the input, then do the math operations and string building, and then print out the output.
• Be sure you explicitly convert input to numerical data before doing any calculations.
SOLUTION (using Python3)
#3 prices
#3 different quantities
priceA = input("Enter the price of item1")
quantityA = input("Enter the quantity of item1")
priceB = input("Enter the price of item2")
quantityB = input("Enter the quantity of item2")
priceC = input("Enter the price of item3")
quantityC = input("Enter the quantity of item3")
subtotal = int(priceA) + int(priceB) + int(priceC)
print(subtotal)
#print("Subtotal:" + " " + "$" + str(subtotal))
#Put tax as an array of different integers?
#so tax will vary once array is rotating?
#should tax be an input?
tax = input("Enter tax amount")
#print("Tax:" + " " + "$" + str(tax))
#total incl. subtotal & tax
#realTotal = int(subtotal) + int(tax)
realTotal = str(subtotal) + str(tax)
print("Subtotal:" + " " + "$" + str(subtotal))
print("Tax:" + " " + "$" + str(tax))
print("Total:" + " " + "$" + str(realTotal))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment