Int / Float Project Solutions
Level 1: Mini Calculator
Level 2: Converter Lab
Level 3: Split the Bill
Level 1: Mini Calculator
Goal: Ask the user for 2 numbers, print the sum, difference, product, and quotient (rounded to 2 places)
Step 1: Ask the user for two numbers
a_str = input("Enter the first number: ")
b_str = input("Enter the second number: ")
We store the user's input as strings first, since input() always returns text.
Step 2: Convert the inputs to floats
a = float(a_str)
b = float(b_str)
We convert the strings into numbers using float() so the user can type decimals, or if you want to keep them as whole numbers, use int().
Step 3: Calculate sum, difference, and product
sum = a + b
diff = a - b
prod = a * b
Calculate the sum, diff, and prod variables normally, using the arithmetic operations +, -, and *.
Step 4: Calculate division safely
if b == 0:
quot = "undefined"
else:
quote = a / b
Division by 0 is not allowed, so we have to check first. If b is 0, give quot the value "undefined", and if not, compute normally.
Step 5: Print results
print("Sum:", sum)
print("Difference:", diff)
print("Product:", prod)
print("Quotient:", quot)
We print each result using commas to insert spaces between the strings and variables.
Level 2: Converter Lab
Goal: Ask the user for a number, and convert it to different units (Celsius to Fahrenheit, km to mi, lbs to kg)
Step 1: Show the choices to the user
print("Converter Lab")
print("1. Celsius to Fahrenheit")
print("2. Kilometers to Miles")
print("3. Pounds to Kilograms)
We print a menu so the user knows what conversions are available.
Step 2: Ask for the choice and the value
choice = input("Choose 1, 2, or 3: ")
value = float(input("Enter the value to convert: ")
We store the conversion the user wants with choice, and value is a float (or integer, if you use int()) containing the input value from the user.
Step 3: Use if/elif/else to do the correct formula
if choice == "1":
result = (value * 9 / 5) + 32
print(value + " C = " + result + " F")
elif choice == "2":
result = value * 0.621371
print(value + " km = " + result + " mi")
elif choice == "3":
result = value * 0.453592
print(value + " lbs = " + result + " kg")
else:
print("invalid choice")
Each conversion uses the standard formula: F = C * 9 / 5 + 32, miles = km * 0.621371, kg = lbs * 0.453592. We use if/elif/else to determine which formula to use from the user's choice, and if the user enters anything other than 1, 2, or 3, we print "invalid choice".
Level 3: Split the Bill
Goal: Ask the user for a bill total, tax rate, tip rate, and the number of people, and output how much each person has to pay. Also, print whether anyone has to pay extra, and how much.
Step 1: Ask the user for bill, tax rate, tip rate, and people
bill = float(input("Bill total: "))
tax = float(input("Tax rate (%): "))
tip = float(input("Tip rate (%): "))
people = int(input("Number of people: "))
bill is the pre-tax, pre-tip total. tax/tip are percentages, and bill needs decimals, so they are floats. people is an integer.
Step 2: Convert percentages to decimals
tax /= 100
tip /= 100
Use the assignment operator /= to convert percentages to decimals.
Step 3: Compute tax, tip, and grand total
tax_amt = bill * tax
subtotal = bill + tax_amt
tip_amt = subtotal * tip
grand_total = subtotal + tip_amt
tax_amt calculates how much the tax is, subtotal is the bill after tax, tip_amt is based on the amount after tax (you can change this if you want to tip before tax), and grand_total is the subtotal + the tip amount.
Step 4: Compute split per person (rounded)
each = round(grand_total / people, 2)
We use the round() function to determine the split and round to 2 decimals because money uses cents.
Step 5: Determine if anyone has to pay extra, and if so, how much
grand_total_rounded = round(grand_total, 2)
total_collected = round(each * people, 2)
difference = round(total_collected - grand_total_rounded, 2)
We calculate total collected from the amount owed by each person and the number of people, and we round both the total collected and the grand total to 2 decimal places. We then compute the difference, and if difference == 0 then nobody pays extra, if the difference > 0, the group pays a few cents extra, if difference < 0, the group is short a few cents.
Step 6: Print how much each person pays and whether anyone pays extra
print("Each person pays:", each)
if difference == 0:
print("No one pays extra.")
elif difference > 0:
print("The group pays " + difference + " extra total.")
else:
print("The group is short " + abs(difference) + " total.")
We print out how much each person pays, then depending on whether difference is 0, positive, or negative, we print out that either no one pays extra, the group pays a bit extra, or the group is missing some amount and someone needs to pay extra. When difference is negative, we use the abs() function to make difference positive.