📖 Scenario: You are programming a delivery drone that carries packages to different locations in a city. The drone needs to know the packages it carries, the maximum weight it can carry, and then calculate the total weight of the packages to check if it is within the limit.
🎯 Goal: Build a simple program that stores package weights, sets a maximum weight limit, calculates the total weight of packages, and prints whether the drone can carry all packages safely.
📋 What You'll Learn
Create a dictionary with package names and their weights
Create a variable for the drone's maximum weight capacity
Calculate the total weight of all packages using a loop
Print a message showing the total weight and if it is within the limit
💡 Why This Matters
🌍 Real World
Delivery drones need to manage their load carefully to avoid accidents and ensure safe delivery.
💼 Career
Understanding how to manage data and conditions is important for programming drones and other automated delivery systems.
Progress0 / 4 steps
1
Create the package weights dictionary
Create a dictionary called packages with these exact entries: 'PackageA': 2.5, 'PackageB': 1.0, 'PackageC': 3.0
Drone Programming
Hint
Use curly braces to create a dictionary with keys as package names and values as weights.
2
Set the drone's maximum weight capacity
Create a variable called max_weight and set it to 7.0 to represent the drone's maximum weight capacity in kilograms.
Drone Programming
Hint
Just assign the number 7.0 to the variable max_weight.
3
Calculate the total weight of packages
Create a variable called total_weight and set it to 0. Then use a for loop with variables package and weight to iterate over packages.items(). Inside the loop, add weight to total_weight.
Drone Programming
Hint
Start with total_weight = 0. Then add each package's weight to total_weight inside the loop.
4
Print the total weight and check capacity
Write a print statement that shows the total weight using an f-string: print(f"Total weight: {total_weight} kg"). Then write an if statement to check if total_weight is less than or equal to max_weight. If yes, print "Drone can carry all packages safely." else print "Drone cannot carry all packages safely."
Drone Programming
Hint
Use an f-string to print total_weight. Then use an if-else to print the correct message based on the weight check.
Practice
(1/5)
1. What is the main purpose of a delivery drone in programming terms?
easy
A. To carry packages from one place to another automatically
B. To play music while flying
C. To take photos of landscapes
D. To clean the air by flying
Solution
Step 1: Understand the drone's role
A delivery drone is designed to transport packages safely and efficiently.
Step 2: Match the purpose to options
Only To carry packages from one place to another automatically describes carrying packages automatically, which fits delivery.
Final Answer:
To carry packages from one place to another automatically -> Option A
Quick Check:
Delivery drone = carry packages [OK]
Hint: Delivery drones move packages, not play or clean [OK]
Common Mistakes:
Confusing drone delivery with photography
Thinking drones clean air
Assuming drones play music
2. Which of the following is the correct way to set a package in a drone program?
easy
A. drone.package = 'Box'
B. drone->setPackage('Box')
C. setPackage(drone, 'Box')
D. drone.setPackage('Box')
Solution
Step 1: Identify method call syntax
In drone programming, methods are called on objects using dot notation like drone.setPackage('Box').
Step 2: Check each option
drone.setPackage('Box') uses correct method call syntax. drone.package = 'Box' assigns directly, which may not be allowed. setPackage(drone, 'Box') uses wrong function call style. drone->setPackage('Box') uses incorrect arrow syntax.
Final Answer:
drone.setPackage('Box') -> Option D
Quick Check:
Method call uses dot notation [OK]
Hint: Use dot notation for methods like setPackage() [OK]
Common Mistakes:
Using assignment instead of method call
Using arrow (->) instead of dot (.)
Calling method with wrong argument order
3. What will be the output of this drone program snippet?
Assuming flyTo moves drone but delivery requires destination set first.
medium
A. flyTo() should be flyToDestination()
B. Package not set before delivery
C. Missing destination set before flying
D. deliver() method does not exist
Solution
Step 1: Check method order and requirements
Delivery requires setting destination before flying. Here, destination is not set explicitly.
Step 2: Identify missing step
Code calls flyTo('Office') but does not set destination property, which delivery depends on.
Final Answer:
Missing destination set before flying -> Option C
Quick Check:
Destination must be set before flyTo and deliver [OK]
Hint: Always set destination before flying and delivering [OK]
Common Mistakes:
Skipping destination setup
Assuming flyTo sets destination automatically
Confusing method names
5. You want to program a drone to deliver multiple packages to different destinations in sequence. Which approach best fits this task?
hard
A. Use a loop to set each package and destination, then fly and deliver each time
B. Set all packages at once and fly to one destination only
C. Fly randomly and drop packages without setting destinations
D. Deliver packages without flying
Solution
Step 1: Understand multi-package delivery needs
Each package must be assigned a destination and delivered one by one.
Step 2: Choose correct programming structure
A loop allows repeating setPackage, setDestination, fly, and deliver steps for each package.
Step 3: Evaluate other options
Set all packages at once and fly to one destination only ignores multiple destinations. Fly randomly and drop packages without setting destinations is unsafe and random. Deliver packages without flying skips flying, which is needed.
Final Answer:
Use a loop to set each package and destination, then fly and deliver each time -> Option A
Quick Check:
Loop for sequential deliveries [OK]
Hint: Loop through packages and destinations for multiple deliveries [OK]
Common Mistakes:
Trying to deliver all packages at once
Ignoring destination assignment
Skipping flying step
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.