
Code
Lantern Calculator
Let's combine everything into a tiny calculator. The trick is to convert input right away:
boxes = int(input("How many boxes? "))
price = float(input("Price per box? "))
print("Total:", boxes * price)
Read the first line from the inside out: input(...) gets the
text, int(...) turns it into a number, then = stores it.
Use int() for things you count (lanterns, people) and
float() for things that can have decimals (litres, prices).
Assignment
The Input box has two lines: 12 and 3.
- Ask
Lanterns on the island?and store it as an int inlanterns. - Ask
Hours each lantern burns?and store it as an int inhours. - Set
total_hourstolanternstimeshours. - Print
Total lantern-hours:and the number, using a comma.
Expected output:
Lanterns on the island? 12
Hours each lantern burns? 3
Total lantern-hours: 36The Input box has two lines: 12 and 3.
- Ask
Lanterns on the island?and store it as an int inlanterns. - Ask
Hours each lantern burns?and store it as an int inhours. - Set
total_hourstolanternstimeshours. - Print
Total lantern-hours:and the number, using a comma.
Expected output:
Lanterns on the island? 12
Hours each lantern burns? 3
Total lantern-hours: 36main.py Python sleeping
Press Run to try your code, then Check to test it.