Course on Internet of Things
print \x93Hello World!\x94
read in a text using input() and print it
Calculate

Calculate sin(30\xB0)
If you see errors, how do you correct them? Are the results correct?
a+b
a-b
a*b
a/b
If this happens:
when the user mistypes and enters something that is not a real number, can you capture the error and simply ask the user to repeat his input, until you get 2 correct floating point numbers?
https://iotworkshop.africa/pub/IoT_Course_English/REPLAndStandardPythonProgramming/calculatorV2.py.txt
Bonus points: Writing a parser for user input is rather easy if the numbers and operator are separated by spaces (have a look at the split method of strings). It is quite a bit more tricky if you allow entering calculations without spaces between the operands and the operator. Can you write a parser accomplishing this?
Hint: A very elegant solution to this is the use of regular expressions and Python provides a module helping you with this:
https://docs.micropython.org/en/latest/library/ure.html
A float is defined as follows: zero or more spaces
followed be zero of more spaces
| Input | ok or not ok |
|---|---|
| 5.3+4.7 | ok |
| 5.3++4.7 | not ok |
| 5..3 + 4.7 | not ok |
| .3+.7 | ok |
| 5.3+4b7 | not ok |
| empty string | not ok |
| 5.3+. | not ok |
| 5.3+ | not ok |
| -1--1 | ok |
| --1--1 | not ok |
Add these series to your class and try. Write a test script exercising the different methods.
Please also have a look at the Wikipedia page on number series: https://en.wikipedia.org/wiki/Series_(mathematics)
In order to make this exercise run on the ESP32 you must first upload your mathSeries.py module implementing the MathSeries class into the file system of the ESP32. Create a directory called lib on the ESP32 file system and put your module there:
ampy mkdir /lib
ampy put mathSeries.py /lib/mathSeries.py
Once this is done, go to the REPL window of thonny and
import mathSeries
You can now create an instance of the MathSeries class:
series = mathSeries.MathSeries()
When running on a PC you may want to plot the series as well. If you don't know how to plot data in Python, you will find all the information needed here:| I | Attachment | Action | Size | Date | Who | Comment |
|---|---|---|---|---|---|---|
| |
calcOk.png | manage | 21 K | 03 May 2020 - 19:52 | UnknownUser | |
| |
calculatorV1.py.txt | manage | 438 bytes | 28 May 2020 - 09:34 | UnknownUser | |
| |
calculatorV2.py.txt | manage | 1 K | 28 May 2020 - 09:34 | UnknownUser | |
| |
calculatorV3.py.txt | manage | 3 K | 28 May 2020 - 09:34 | UnknownUser | |
| |
conditions.png | manage | 28 K | 03 May 2020 - 20:25 | UnknownUser | |
| |
conversionError.png | manage | 19 K | 03 May 2020 - 19:42 | UnknownUser | |
| |
e.png | manage | 1 K | 08 Feb 2022 - 16:45 | UnknownUser | |
| |
euler.png | manage | 1 K | 08 Feb 2022 - 16:50 | UnknownUser | |
| |
exercise_1.odt | manage | 126 K | 08 Feb 2022 - 17:45 | UnknownUser | |
| |
factorial.png | manage | 2 K | 04 May 2020 - 12:48 | UnknownUser | |
| |
geometric.png | manage | 3 K | 04 May 2020 - 12:48 | UnknownUser | |
| |
harmonic.png | manage | 1 K | 04 May 2020 - 12:53 | UnknownUser | |
| |
invalidFloat.png | manage | 31 K | 08 May 2020 - 13:38 | UnknownUser | |
| |
ln2.png | manage | 1 K | 04 May 2020 - 12:48 | UnknownUser | |
| |
mathSeries.png | manage | 9 K | 08 Feb 2022 - 17:36 | UnknownUser | |
| |
pi.png | manage | 2 K | 04 May 2020 - 12:48 | UnknownUser | |
| |
wallis.png | manage | 6 K | 05 May 2020 - 14:31 | UnknownUser | |
| |
wallis1.png | manage | 14 K | 05 May 2020 - 14:35 | UnknownUser | |
| |
wallis_prod.png | manage | 4 K | 27 Jan 2022 - 09:46 | UnknownUser |