Input and Output Checkpoint
A review of this module. Skip it if you feel confident; the next module does not need it.
Fix the mistakes
The slips that cost marks in this module. Spot each one, then check the fix.
Consolidate your input and output skills by creating clean, professional terminal receipts and formatted outputs.
Formatting tabular receipts
Using width specifiers in f-strings lets you create aligned tables and receipts on the command line.
print(f"{'Item':<15} {'Qty':>5} {'Price':>10}")print("-" * 32)print(f"{'Keyboard':<15} {2:>5} {'$120.00':>10}")Example 1 · Tax invoice
customer = "Rajesh"price = 14500.0gst = price * 0.18total = price + gstprint(f"Customer: {customer}")print(f"Subtotal: Rs. {price:,.2f}")print(f"GST (18%): Rs. {gst:,.2f}")print(f"Total: Rs. {total:,.2f}")Customer: Rajesh Subtotal: Rs. 14,500.00 GST (18%): Rs. 2,610.00 Total: Rs. 17,110.00
What this code does
- Calculates the 18% GST and the total bill.
- Uses
,and.2ffor professional currency formatting.
The <15 aligns text left with 15 characters width, while >5 and >10 align numbers neatly to the right.
Dynamic separators in logs
Combine timestamps and logs cleanly using sep and end without clumsy concatenation.
print("[INFO]", "Server started on port", 8000, sep=" | ")Example 2 · A log line with sep and end
print("[INFO]", "Server started on port", 8000, sep=" | ")print("Checking", end="... ")print("ready")[INFO] | Server started on port | 8000 Checking... ready
What this code does
sepplaces a bar between the three values.end="... "keeps the next print on the same line.
Example 3 · Receipt from typed answers
item = input("Item: ")qty = int(input("Qty: "))price = float(input("Price: "))print(f"{'Item':<12}{'Qty':>5}{'Amount':>12}")print("-" * 29)print(f"{item:<12}{qty:>5}{qty * price:>12,.2f}")Item: Monitor Qty: 2 Price: 6499.5 Item Qty Amount ----------------------------- Monitor 2 12,999.00
What this code does
input()text is converted withint()andfloat()before the multiplication.- The same widths in the header and the row keep the columns aligned.
The output prints [INFO] | Server started on port | 8000.
Finish up
Mark complete & continue
Next: The if and if-else Statements