Build and manage your own virtual robot assistant, giving it tasks and keeping it charged!
Today, you'll bring your robot to life and give it a unique role!
- Create a new Python file named
robot_manager.py
. - Ask for your robot's name. Use the
input()
function to get a name from the user and store it in a variable (e.g.,robot_name
). - Ask for your robot's role. Use
input()
to ask "What kind of helper is [Robot Name]? (e.g., cleaner/friend/guard)" and store it in a variable (e.g.,robot_role
). - Set your robot's starting
battery_level
to100
. Store this in another variable. - Print a welcoming message using an f-string or string concatenation. It should include your robot's name, its role, and its current battery level.
- Example Input: "Bolt", "cleaner"
- Example Output: "Hello! I am Bolt, your new cleaner helper! My battery is 100%."
Your robot will perform its very first useful task!
- Define a function called
perform_task(task_name, battery_cost, current_battery_level, robot_name)
.- This function needs to know the
task_name
, how muchbattery_cost
it takes, the robot'scurrent_battery_level
(passed in as an argument), and therobot_name
. - It should
print()
a message like "[Robot Name] is [task_name]." - It should then decrease the
current_battery_level
by thebattery_cost
. - Finally, it should
print()
the robot's updatedcurrent_battery_level
and return the new battery level (so the main part of your program knows the updated value).
- This function needs to know the
- Create a
while True
loop that will keep your robot active throughout the day. - Inside the loop, ask the user: "What should [Robot Name] do? (clean/nothing/end_day)"
- Use
if/elif
statements:- If the user types "clean", call
perform_task("cleaning the room", 10, battery_level, robot_name)
. Important: The value thatperform_task
returns will be the robot's new battery level, so make sure to update yourbattery_level
variable with this returned value (e.g.,battery_level = perform_task(...)
). - If the user types "nothing", print a polite message like "Okay, resting for now."
- If the user types "end_day", use
break
to exit thewhile
loop.
- If the user types "clean", call
- After the
while
loop finishes, print a message saying the day is over and show the robot's finalbattery_level
.- Example Input: "clean"
- Example Output: "Bolt is cleaning the room. Battery left: 90%"
Your robot needs more options and a way to refuel!
- Modify your
while
loop's question to include new options: "What should [Robot Name] do? (clean/wash dishes/charge/nothing/end_day)" - Add a new task option: "wash dishes".
- Modify your
while
loop'sif/elif
structure. If the user types "wash dishes", callperform_task("washing dishes", 15, battery_level, robot_name)
. Remember to update yourbattery_level
variable with the returned value!
- Modify your
- Create a new function
charge_robot(current_battery_level, robot_name)
:- This function should take the
current_battery_level
androbot_name
as arguments. - It should increase the
current_battery_level
by 30. - Important: Make sure
current_battery_level
never goes above 100! Use anif
statement to check and set it to 100 if the increase would push it higher. - Print a message like "[Robot Name] is charging. Battery increased to [level]%."
- Return the new (updated) battery level.
- This function should take the
- Add a new option in your
while
loop: "charge". If the user types "charge", call thecharge_robot(battery_level, robot_name)
function. Again, update yourbattery_level
variable with the returned value. - (Open-ended creativity): Add one more simple task of your own choice!
- For example, "fetch item", "sing song", or "tell joke".
- Decide how much
battery_cost
this new task will have (e.g., 5, 20). - Add this new option to your
while
loop'sif/elif
structure, callingperform_task
and updatingbattery_level
. - Example Input: "charge"
- Example Output: "Bolt is charging. Battery increased to 100%."
Robots need to be careful with their power! Let's make sure your robot doesn't try to do things without enough battery, and can tell you its status.
- Modify your
perform_task
function:- Before deducting battery: Add an
if
statement. If thecurrent_battery_level
is currently less than thebattery_cost
for the task:print()
a message like "[Robot Name] doesn't have enough battery for that task!"- Then,
return current_battery_level
. This is important because it stops the function and sends back the original battery level, showing that no change happened.
- Only if there's enough battery should the rest of the function (deducting battery, printing new level, returning new level) happen.
- Before deducting battery: Add an
- Modify your
while
loop's question to include a "status" option: "What should [Robot Name] do? (clean/wash dishes/charge/status/nothing/end_day)" - Add a new command: "status"
- In your
while
loop, add a newelif
condition. If the user types "status":print()
a message like "Current battery: [battery_level]%."- This command should not change the
battery_level
, so you just print it; you don't need to call a function or updatebattery_level
.
- In your
- (Open-ended creativity): Make your robot's output a bit more lively!
- For example, when
battery_level
is low (e.g., below 20%), maybe it prints "[Robot Name] is feeling tired..." when you ask it to do something, or if battery is full, "[Robot Name] zips excitedly!" when it finishes charging. - This is just about adding extra
print()
messages based on battery level, not new variables. You'll addif
statements in your existingperform_task
orcharge_robot
functions, or directly in thewhile
loop.
- For example, when
Let's summarize the robot's day and add a unique touch!
- Introduce a new variable
tasks_completed
:- Initialize it to
0
at the very beginning of your script (before thewhile
loop starts).
- Initialize it to
- Update
tasks_completed
:- Inside your
while
loop, for each task that callsperform_task
:- You need to check if the task actually finished (meaning, if the battery went down).
- Hint: Store the
battery_level
in a temporary variable before callingperform_task
. Then, after callingperform_task
and updatingbattery_level
, compare the newbattery_level
to the old temporary one. If the new one is lower, it means a task was successfully completed, so you can increasetasks_completed
by1
.
- Inside your
- Daily Report: At the end of your daily
while
loop (after "end_day"), print a simple summary of the day. For example:- "Today, [Robot Name] completed [tasks_completed] tasks. Final Battery: [battery_level]%."
- (Open-ended creativity): Add one more small, new interaction to your robot!
- Option A: "greet" command: Add this as an option in your
while
loop. If the user types "greet", print a custom greeting like "Hello, Master!" or "Pleased to serve you!". This command should not cost battery. - Option B: "joke" command: Add this as an option. If the user types "joke", print a simple, pre-written joke. This command should not cost battery.
- Or, invent your own unique small interaction! Think of a command that might print something fun or interact with the robot's state in a very small, simple way.
- Option A: "greet" command: Add this as an option in your