100751 Views
79190 Views
45286 Views
43314 Views
39973 Views
33349 Views
NextCloud
Buddy Jr.
Robot Arms
TherePi - Music Making with a Raspberry Pi
Motion Controlled Arm
Electric Trombone
Building a Robot Arm with Raspberry Pi and PCA9685
Building User Authentication for Static Sites with FastAPI
Mastering Pydantic for Robust Data Validation
Mastering Markdown for Documentation with Jekyll
Introduction to Rust
OctaPi
KevsRobots Learning Platform
55% Percent Complete
By Kevin McAleer, 2 Minutes
Letās do something more interesting.
Weāll make the Raspberry Pi Pico (or whichever board you are using) ask us a question, then reply back to us.
Type of the program below into your Python Editor
# Variables # name.py print("Please type your name:") name = input() print("Hello", name)
Save the file and then run it by pressing the green run button.
run
The name variable holds the value that it is assigned, and we assigned it by using the = equals sign.
name
=
The function input, is another built-in function that MicroPython provides to get user input from the keyboard and return it to our program.
input
The variable is like a box that we can put things in, in this case a persons name.
We can also store numbers in variables; letās extend our example and add an extra question
numbers
type:
name = input('hello, please enter your name > ') age = input('please enter your age > ') print('hello', name, 'how does', age, 'feel?')
Note input() only returns text (str) types of data, this means if you try to do any math with the age it will not work in the way you expect. If you want age to be a number you will too to wrap it in the int() type to change it. E.g. age = int(input('please enter your age >')) print('next year you will be', age + 1, 'years old') This is called casting and weāll look at this in more depth in later modules.
input() only returns text (str) types of data, this means if you try to do any math with the age it will not work in the way you expect.
input()
str
If you want age to be a number you will too to wrap it in the int() type to change it.
age
int()
E.g.
age = int(input('please enter your age >')) print('next year you will be', age + 1, 'years old')
This is called casting and weāll look at this in more depth in later modules.
casting
We can use variables in maths, for example if we want to find the missing angle in a triangle, and we have the other two angles we can use a formula: 180 - a + b = c
180 - a + b = c
a = 65 b = 42 c = 180 - a + b print('the missing angle is: ', c)
< Previous Next >