Lesson 2
The Basic Elements
Chapter 8: Robot project
Creating your robot
Now, create a file called main.py
in the robot directory.
Your task now is to create your first robot.
We want to allow the user to enter a nice name for your robot when you run this program. Use input()
to read this name from the user, like in our guessing game from Lesson 1. Once you have read it, assign it to the variable called name
.
Now, also assign an identifier
to the robot once it has been named, so that you can distinguish it from other robots in the future. Make this identifier
an integer. Let’s just give it an identifier
of 1000
(or whatever you like).
You program will then print out a message from the robot with print()
. The message should contain the name and the identifier of the robot. For example, the message can be "Hello. My name is Bender. My ID is 1000."
.
You can format a string using Python f-strings
(only available for Python \ge 3.6). If you put an f
before your start quote for a string, you can directly embed variables inside the string with curly braces {}
. Here is an example of how to use f-strings.
>>> name = "Luca"
>>> age = 10
>>> message = f"{name} is {age} years old."
>>> print(message)
Luca is 10 years old.
Run your program, and make sure that it is working correctly.
Sample run
What is the name of the robot? Bender
Hello. My name is Bender. My ID is 1000.