Difference: REPLAndStandardPythonProgramming (1 vs. 17)

Revision 172022-02-08 - UliRaich

Line: 1 to 1
 
META TOPICPARENT name="Exercises"

Course on Internet of Things

Exercises Session 1:

Line: 99 to 99
 
  • The geometric number series:
    geometric.png
  • The harmonic number series:
    harmonic.png
    Can you tell what the following series calculate?
Changed:
<
<
ln2.png pi.png

wallis_prod.png

e.png
>
>
mathSeries.png
 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)

Line: 138 to 139
 
META FILEATTACHMENT attachment="ln2.png" attr="" comment="" date="1588596529" name="ln2.png" path="ln2.png" size="2046" user="UliRaich" version="1"
META FILEATTACHMENT attachment="pi.png" attr="" comment="" date="1588596529" name="pi.png" path="pi.png" size="2785" user="UliRaich" version="1"
META FILEATTACHMENT attachment="harmonic.png" attr="" comment="" date="1588596798" name="harmonic.png" path="harmonic.png" size="1631" user="UliRaich" version="1"
Changed:
<
<
META FILEATTACHMENT attachment="e.png" attr="" comment="" date="1588596991" name="e.png" path="e.png" size="1931" user="UliRaich" version="1"
>
>
META FILEATTACHMENT attachment="e.png" attr="" comment="" date="1644338724" name="e.png" path="e.png" size="1554" user="UliRaich" version="2"
 
META FILEATTACHMENT attachment="wallis.png" attr="" comment="" date="1588689113" name="wallis.png" path="wallis.png" size="6212" user="UliRaich" version="1"
META FILEATTACHMENT attachment="wallis1.png" attr="" comment="" date="1588689317" name="wallis1.png" path="wallis1.png" size="15114" user="UliRaich" version="1"
META FILEATTACHMENT attachment="invalidFloat.png" attr="" comment="" date="1588945107" name="invalidFloat.png" path="invalidFloat.png" size="32744" user="UliRaich" version="1"
META FILEATTACHMENT attachment="calculatorV1.py.txt" attr="" comment="" date="1590658465" name="calculatorV1.py.txt" path="calculatorV1.py.txt" size="438" user="UliRaich" version="1"
META FILEATTACHMENT attachment="calculatorV2.py.txt" attr="" comment="" date="1590658465" name="calculatorV2.py.txt" path="calculatorV2.py.txt" size="1363" user="UliRaich" version="1"
META FILEATTACHMENT attachment="calculatorV3.py.txt" attr="" comment="" date="1590658465" name="calculatorV3.py.txt" path="calculatorV3.py.txt" size="3255" user="UliRaich" version="1"
Changed:
<
<
META FILEATTACHMENT attachment="exercise_1.odt" attr="" comment="" date="1596961520" name="exercise_1.odt" path="exercise_1.odt" size="135777" user="UliRaich" version="1"
>
>
META FILEATTACHMENT attachment="exercise_1.odt" attr="" comment="" date="1644342317" name="exercise_1.odt" path="exercise_1.odt" size="129592" user="UliRaich" version="2"
 
META FILEATTACHMENT attachment="wallis_prod.png" attr="" comment="" date="1643276815" name="wallis_prod.png" path="wallis_prod.png" size="4345" user="UliRaich" version="4"
Added:
>
>
META FILEATTACHMENT attachment="euler.png" attr="" comment="" date="1644339013" name="euler.png" path="euler.png" size="1554" user="UliRaich" version="1"
META FILEATTACHMENT attachment="mathSeries.png" attr="" comment="" date="1644341778" name="mathSeries.png" path="mathSeries.png" size="9917" user="UliRaich" version="3"

Revision 162022-01-27 - UliRaich

Line: 1 to 1
 
META TOPICPARENT name="Exercises"

Course on Internet of Things

Exercises Session 1:

Line: 99 to 99
 
  • The geometric number series:
    geometric.png
  • The harmonic number series:
    harmonic.png
    Can you tell what the following series calculate?
Changed:
<
<
ln2.png pi.png

wallis_prod.png

wallis1.png

e.png
>
>
ln2.png pi.png

wallis_prod.png

e.png
 Add these series to your class and try. Write a test script exercising the different methods.
Added:
>
>
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

Line: 144 to 146
 
META FILEATTACHMENT attachment="calculatorV2.py.txt" attr="" comment="" date="1590658465" name="calculatorV2.py.txt" path="calculatorV2.py.txt" size="1363" user="UliRaich" version="1"
META FILEATTACHMENT attachment="calculatorV3.py.txt" attr="" comment="" date="1590658465" name="calculatorV3.py.txt" path="calculatorV3.py.txt" size="3255" user="UliRaich" version="1"
META FILEATTACHMENT attachment="exercise_1.odt" attr="" comment="" date="1596961520" name="exercise_1.odt" path="exercise_1.odt" size="135777" user="UliRaich" version="1"
Changed:
<
<
META FILEATTACHMENT attachment="wallis_prod.png" attr="" comment="" date="1642790907" name="wallis_prod.png" path="wallis_prod.png" size="13562" user="UliRaich" version="2"
>
>
META FILEATTACHMENT attachment="wallis_prod.png" attr="" comment="" date="1643276815" name="wallis_prod.png" path="wallis_prod.png" size="4345" user="UliRaich" version="4"

Revision 152022-01-21 - UliRaich

Line: 1 to 1
 
META TOPICPARENT name="Exercises"

Course on Internet of Things

Exercises Session 1:

Line: 99 to 99
 
  • The geometric number series:
    geometric.png
  • The harmonic number series:
    harmonic.png
    Can you tell what the following series calculate?
Changed:
<
<
ln2.png pi.png

wallis1.png

e.png
>
>
ln2.png pi.png

wallis_prod.png

wallis1.png

e.png
 Add these series to your class and try. Write a test script exercising the different methods.

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:

Line: 144 to 144
 
META FILEATTACHMENT attachment="calculatorV2.py.txt" attr="" comment="" date="1590658465" name="calculatorV2.py.txt" path="calculatorV2.py.txt" size="1363" user="UliRaich" version="1"
META FILEATTACHMENT attachment="calculatorV3.py.txt" attr="" comment="" date="1590658465" name="calculatorV3.py.txt" path="calculatorV3.py.txt" size="3255" user="UliRaich" version="1"
META FILEATTACHMENT attachment="exercise_1.odt" attr="" comment="" date="1596961520" name="exercise_1.odt" path="exercise_1.odt" size="135777" user="UliRaich" version="1"
Added:
>
>
META FILEATTACHMENT attachment="wallis_prod.png" attr="" comment="" date="1642790907" name="wallis_prod.png" path="wallis_prod.png" size="13562" user="UliRaich" version="2"

Revision 142021-06-26 - UliRaich

Line: 1 to 1
 
META TOPICPARENT name="Exercises"

Course on Internet of Things

Exercises Session 1:

Revision 132021-02-18 - TWikiAdminUser

Line: 1 to 1
 
META TOPICPARENT name="Exercises"

Course on Internet of Things

Exercises Session 1:

Line: 12 to 12
 Using REPL:
  • print “Hello World!”

  • read in a text using input() and print it

Changed:
<
<
  • Calculate

    calculations.png

>
>
  • Calculate

    calculations.png

 
  • Calculate sin(30°)
    If you see errors, how do you correct them? Are the results correct?

  • Finally make the "print Hello World!" command a script and execute it.

Exercise 2: A simple calculator

Revision 122020-08-09 - UliRaich

Line: 1 to 1
 
META TOPICPARENT name="Exercises"

Course on Internet of Things

Exercises Session 1:

Line: 118 to 118
  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:
https://matplotlib.org/3.2.1/index.html
Added:
>
>
Here is the exercise sheet in odt format:

https://afnog.iotworkshop.africa/pub/IoT_Course_English/REPLAndStandardPythonProgramming/exercise_1.odt

 -- Uli Raich - 2020-05-03

Comments

Line: 139 to 143
 
META FILEATTACHMENT attachment="calculatorV1.py.txt" attr="" comment="" date="1590658465" name="calculatorV1.py.txt" path="calculatorV1.py.txt" size="438" user="UliRaich" version="1"
META FILEATTACHMENT attachment="calculatorV2.py.txt" attr="" comment="" date="1590658465" name="calculatorV2.py.txt" path="calculatorV2.py.txt" size="1363" user="UliRaich" version="1"
META FILEATTACHMENT attachment="calculatorV3.py.txt" attr="" comment="" date="1590658465" name="calculatorV3.py.txt" path="calculatorV3.py.txt" size="3255" user="UliRaich" version="1"
Added:
>
>
META FILEATTACHMENT attachment="exercise_1.odt" attr="" comment="" date="1596961520" name="exercise_1.odt" path="exercise_1.odt" size="135777" user="UliRaich" version="1"

Revision 112020-06-01 - UliRaich

Line: 1 to 1
 
META TOPICPARENT name="Exercises"

Course on Internet of Things

Exercises Session 1:

Line: 46 to 46
  https://docs.micropython.org/en/latest/library/ure.html
Added:
>
>
A float is defined as follows:
  • zero or more spaces

  • followed by one '+' or one '-'
  • followed by
    • a ‘.’ followed by at least one digit
  • or
    • at least one digit followed by zero or one ‘.’ followed by zero or more digits
  • followed be zero of more spaces

 Try your parser on this input:
Input ok or not ok
5.3+4.7 ok

Revision 102020-06-01 - UliRaich

Line: 1 to 1
 
META TOPICPARENT name="Exercises"

Course on Internet of Things

Exercises Session 1:

Line: 42 to 42
  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?
Added:
>
>
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

 Try your parser on this input:
Input ok or not ok
5.3+4.7 ok
Line: 52 to 56
 
empty string not ok
5.3+. not ok
5.3+ not ok
Added:
>
>
-1--1 ok
--1--1 not ok
 Try also several spaces in between operands and operator.

https://afnog.iotworkshop.africa/pub/IoT_Course_English/REPLAndStandardPythonProgramming/calculatorV3.py.txt

Revision 92020-05-28 - UliRaich

Line: 1 to 1
 
META TOPICPARENT name="Exercises"

Course on Internet of Things

Exercises Session 1:

Line: 26 to 26
 
  • a/b

If you use integer format when printing the result, you will see the result truncated to an integer. Can you correct changing the format?
Added:
>
>
https://afnog.iotworkshop.africa/pub/IoT_Course_English/REPLAndStandardPythonProgramming/calculatorV1.py.txt
 Improve this program asking the user to enter 2 real numbers (floats) separated by a space. This is how it should look like:

calcOk.png

Line: 36 to 38
  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?
Added:
>
>
https://afnog.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?

Try your parser on this input:

Line: 49 to 53
 
5.3+. not ok
5.3+ not ok
Try also several spaces in between operands and operator.
Added:
>
>
https://afnog.iotworkshop.africa/pub/IoT_Course_English/REPLAndStandardPythonProgramming/calculatorV3.py.txt
 

Exercise 3: Conditions

Like in exercise2, start with 2 numbers a,b with values 5 and 7. In your program check which of the 2 numbers is bigger and print the result.

Line: 116 to 122
 
META FILEATTACHMENT attachment="wallis.png" attr="" comment="" date="1588689113" name="wallis.png" path="wallis.png" size="6212" user="UliRaich" version="1"
META FILEATTACHMENT attachment="wallis1.png" attr="" comment="" date="1588689317" name="wallis1.png" path="wallis1.png" size="15114" user="UliRaich" version="1"
META FILEATTACHMENT attachment="invalidFloat.png" attr="" comment="" date="1588945107" name="invalidFloat.png" path="invalidFloat.png" size="32744" user="UliRaich" version="1"
Added:
>
>
META FILEATTACHMENT attachment="calculatorV1.py.txt" attr="" comment="" date="1590658465" name="calculatorV1.py.txt" path="calculatorV1.py.txt" size="438" user="UliRaich" version="1"
META FILEATTACHMENT attachment="calculatorV2.py.txt" attr="" comment="" date="1590658465" name="calculatorV2.py.txt" path="calculatorV2.py.txt" size="1363" user="UliRaich" version="1"
META FILEATTACHMENT attachment="calculatorV3.py.txt" attr="" comment="" date="1590658465" name="calculatorV3.py.txt" path="calculatorV3.py.txt" size="3255" user="UliRaich" version="1"

Revision 82020-05-08 - UliRaich

Line: 1 to 1
 
META TOPICPARENT name="Exercises"

Course on Internet of Things

Exercises Session 1:

Line: 32 to 32
  If this happens:
Changed:
<
<
conversionError.png
>
>
invalidFloat.png
 
Changed:
<
<
when the user mistypes and enters something that is not an integer, can you capture the error and simply ask the user to repeat his input, until you get 2 correct integer numbers?
>
>
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?
  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?
Line: 71 to 71
 Write a script that asks the user for an angle in degrees. Calculate the sine of this angle.

Exercise 6: Classes

Changed:
<
<
Write a Python class with methods calculation mathematical number series. These series should be calculated:
>
>
Write a Python class with methods calculating mathematical number series. These series should be calculated:
 
  • The Fibonacci numbers up to F(n). n is passed as parameter to the method
  • The Fibonacci numbers smaller than max. max is passed as parameter to the method
  • The prime numbers up to max
Line: 115 to 115
 
META FILEATTACHMENT attachment="e.png" attr="" comment="" date="1588596991" name="e.png" path="e.png" size="1931" user="UliRaich" version="1"
META FILEATTACHMENT attachment="wallis.png" attr="" comment="" date="1588689113" name="wallis.png" path="wallis.png" size="6212" user="UliRaich" version="1"
META FILEATTACHMENT attachment="wallis1.png" attr="" comment="" date="1588689317" name="wallis1.png" path="wallis1.png" size="15114" user="UliRaich" version="1"
Added:
>
>
META FILEATTACHMENT attachment="invalidFloat.png" attr="" comment="" date="1588945107" name="invalidFloat.png" path="invalidFloat.png" size="32744" user="UliRaich" version="1"

Revision 72020-05-05 - UliRaich

Line: 1 to 1
 
META TOPICPARENT name="Exercises"

Course on Internet of Things

Exercises Session 1:

Line: 47 to 47
 
5.3+4b7 not ok
empty string not ok
5.3+. not ok
Added:
>
>
5.3+ not ok
 Try also several spaces in between operands and operator.

Exercise 3: Conditions

Changed:
<
<
Like in exercise2, start with 2 numbers a,b with values 5 and 3. In your program check which of the 2 numbers is bigger and print the result.
>
>
Like in exercise2, start with 2 numbers a,b with values 5 and 7. In your program check which of the 2 numbers is bigger and print the result.
  Then improve the program asking the user for 2 integer numbers. Make sure he enters 2 correct numbers and capture possible errors. This is a typical output:
Line: 65 to 66
 
  • Calculate and print out the Fibonacci numbers up to F(n). Get n from the user where n >= 0
Try your program for n=0, n=1, n=2, n=3, n=20

  • Ask the user up to which is the maximum number up to which you should calculate.
Changed:
<
<

Exercise 5: Some maths: Calculate the sine function

>
>

Exercise 5: A bit of Mathematics: Calculate the sine function

  Write a script that asks the user for an angle in degrees. Calculate the sine of this angle.

Exercise 6: Classes

Line: 81 to 82
 
ln2.png pi.png

wallis1.png

e.png
Add these series to your class and try. Write a test script exercising the different methods.
Changed:
<
<
When running on a PC you may want to plot the series as well.
>
>
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:
https://matplotlib.org/3.2.1/index.html

  -- Uli Raich - 2020-05-03

Revision 62020-05-05 - UliRaich

Line: 1 to 1
 
META TOPICPARENT name="Exercises"

Course on Internet of Things

Exercises Session 1:

Line: 24 to 24
 
  • a-b

  • a*b

  • a/b

Changed:
<
<
Which result do you observe for division? Can you correct?
>
>
If you use integer format when printing the result, you will see the result truncated to an integer. Can you correct changing the format?
  Improve this program asking the user to enter 2 real numbers (floats) separated by a space. This is how it should look like:
Line: 78 to 78
 
  • The geometric number series:
    geometric.png
  • The harmonic number series:
    harmonic.png
    Can you tell what the following series calculate?
Changed:
<
<
ln2.png pi.png e.png
>
>
ln2.png pi.png

wallis1.png

e.png
 Add these series to your class and try. Write a test script exercising the different methods.

When running on a PC you may want to plot the series as well.

Line: 98 to 98
 
META FILEATTACHMENT attachment="pi.png" attr="" comment="" date="1588596529" name="pi.png" path="pi.png" size="2785" user="UliRaich" version="1"
META FILEATTACHMENT attachment="harmonic.png" attr="" comment="" date="1588596798" name="harmonic.png" path="harmonic.png" size="1631" user="UliRaich" version="1"
META FILEATTACHMENT attachment="e.png" attr="" comment="" date="1588596991" name="e.png" path="e.png" size="1931" user="UliRaich" version="1"
Added:
>
>
META FILEATTACHMENT attachment="wallis.png" attr="" comment="" date="1588689113" name="wallis.png" path="wallis.png" size="6212" user="UliRaich" version="1"
META FILEATTACHMENT attachment="wallis1.png" attr="" comment="" date="1588689317" name="wallis1.png" path="wallis1.png" size="15114" user="UliRaich" version="1"

Revision 52020-05-04 - UliRaich

Line: 1 to 1
 
META TOPICPARENT name="Exercises"

Course on Internet of Things

Exercises Session 1:

Introductory remarks:

Changed:
<
<
The scripts we are going to develop during these exercises do not depend on any system issues: We do not use command line arguments not available in MicroPython nor do we use any hardware specific features which are not available on a PC. The exercises can therefore be developed and run on the ESP32 but also on any standard PC with Python3 installed.
>
>
The scripts we are going to develop during this exercise session do not depend on any system issues: We do not use command line arguments not available in MicroPython nor do we use any hardware specific features which are not available on a PC. The exercises can therefore be developed and run on the ESP32 but also on any standard PC with Python3 installed.
 

Exercise 1: Use of REPL

Changed:
<
<
Connect to your ESP32 with minicom or thonny.
>
>
Connect to your ESP32 with minicom or thonny. You may try the same thing running Python3 on your PC.
  Using REPL:
  • print “Hello World!”

Line: 65 to 65
 
  • Calculate and print out the Fibonacci numbers up to F(n). Get n from the user where n >= 0
Try your program for n=0, n=1, n=2, n=3, n=20

  • Ask the user up to which is the maximum number up to which you should calculate.
Added:
>
>

Exercise 5: Some maths: Calculate the sine function

Write a script that asks the user for an angle in degrees. Calculate the sine of this angle.

Exercise 6: Classes

Write a Python class with methods calculation mathematical number series. These series should be calculated:

  • The Fibonacci numbers up to F(n). n is passed as parameter to the method
  • The Fibonacci numbers smaller than max. max is passed as parameter to the method
  • The prime numbers up to max
  • Factorial:
    factorial.png
  • The geometric number series:
    geometric.png
  • The harmonic number series:
    harmonic.png
    Can you tell what the following series calculate?
ln2.png pi.png e.png
Add these series to your class and try. Write a test script exercising the different methods.

When running on a PC you may want to plot the series as well.

  -- Uli Raich - 2020-05-03
Line: 75 to 92
 
META FILEATTACHMENT attachment="conversionError.png" attr="" comment="" date="1588534969" name="conversionError.png" path="conversionError.png" size="19759" user="UliRaich" version="1"
META FILEATTACHMENT attachment="calcOk.png" attr="" comment="" date="1588535543" name="calcOk.png" path="calcOk.png" size="22212" user="UliRaich" version="1"
META FILEATTACHMENT attachment="conditions.png" attr="" comment="" date="1588537519" name="conditions.png" path="conditions.png" size="29528" user="UliRaich" version="1"
Added:
>
>
META FILEATTACHMENT attachment="factorial.png" attr="" comment="" date="1588596529" name="factorial.png" path="factorial.png" size="2855" user="UliRaich" version="1"
META FILEATTACHMENT attachment="geometric.png" attr="" comment="" date="1588596529" name="geometric.png" path="geometric.png" size="3970" user="UliRaich" version="1"
META FILEATTACHMENT attachment="ln2.png" attr="" comment="" date="1588596529" name="ln2.png" path="ln2.png" size="2046" user="UliRaich" version="1"
META FILEATTACHMENT attachment="pi.png" attr="" comment="" date="1588596529" name="pi.png" path="pi.png" size="2785" user="UliRaich" version="1"
META FILEATTACHMENT attachment="harmonic.png" attr="" comment="" date="1588596798" name="harmonic.png" path="harmonic.png" size="1631" user="UliRaich" version="1"
META FILEATTACHMENT attachment="e.png" attr="" comment="" date="1588596991" name="e.png" path="e.png" size="1931" user="UliRaich" version="1"

Revision 42020-05-04 - UliRaich

Line: 1 to 1
 
META TOPICPARENT name="Exercises"

Course on Internet of Things

Exercises Session 1:

Introductory remarks:

The scripts we are going to develop during these exercises do not depend on any system issues: We do not use command line arguments not available in MicroPython nor do we use any hardware specific features which are not available on a PC. The exercises can therefore be developed and run on the ESP32 but also on any standard PC with Python3 installed.

Changed:
<
<

Exercise 1:

>
>

Exercise 1: Use of REPL

  Connect to your ESP32 with minicom or thonny.
Line: 14 to 14
 
  • read in a text using input() and print it

  • Calculate

    calculations.png

  • Calculate sin(30°)
    If you see errors, how do you correct them? Are the results correct?

Changed:
<
<

Exercise 2:

>
>
  • Finally make the "print Hello World!" command a script and execute it.

Exercise 2: A simple calculator

  Write a script that assigns the values 5 and 3 to the variables a and b respectively
Line: 46 to 47
 
5.3+4b7 not ok
empty string not ok
5.3+. not ok
Changed:
<
<
5.3 + 4 ok

Exercise 3:

>
>
Try also several spaces in between operands and operator.

Exercise 3: Conditions

  Like in exercise2, start with 2 numbers a,b with values 5 and 3. In your program check which of the 2 numbers is bigger and print the result.

Then improve the program asking the user for 2 integer numbers. Make sure he enters 2 correct numbers and capture possible errors. This is a typical output:

conditions.png

Added:
>
>

Exercise 4: The Fibonacci series

The Fibonacci number series is defined as:

F(0) = 0; F(1) = 1

F(n) = F(n-1) + F(n-2)

  • Calculate and print out the Fibonacci numbers up to F(n). Get n from the user where n >= 0
Try your program for n=0, n=1, n=2, n=3, n=20

  • Ask the user up to which is the maximum number up to which you should calculate.
  -- Uli Raich - 2020-05-03

Revision 32020-05-04 - UliRaich

Line: 1 to 1
 
META TOPICPARENT name="Exercises"

Course on Internet of Things

Exercises Session 1:

Line: 38 to 38
 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?

Try your parser on this input:

Added:
>
>
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 + 4 ok
 
Deleted:
<
<
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
 

Exercise 3:

Changed:
<
<
Like in exercise2, start with 2 number a,b with values 5 and 3. In your program check which of the 2 numbers is bigger and print the result. Then ask the user for 2 integer numbers. Make sure he enters 2 correct numbers and capture possible errors. This is a typical output:
>
>
Like in exercise2, start with 2 numbers a,b with values 5 and 3. In your program check which of the 2 numbers is bigger and print the result.

Then improve the program asking the user for 2 integer numbers. Make sure he enters 2 correct numbers and capture possible errors. This is a typical output:

  conditions.png

Revision 22020-05-03 - UliRaich

Line: 1 to 1
 
META TOPICPARENT name="Exercises"

Course on Internet of Things

Exercises Session 1:

Added:
>
>

Introductory remarks:

The scripts we are going to develop during these exercises do not depend on any system issues: We do not use command line arguments not available in MicroPython nor do we use any hardware specific features which are not available on a PC. The exercises can therefore be developed and run on the ESP32 but also on any standard PC with Python3 installed.

 

Exercise 1:

Connect to your ESP32 with minicom or thonny.

Line: 22 to 25
 
  • a/b

Which result do you observe for division? Can you correct?
Changed:
<
<
Improve this program asking the user to enter 2 integers separated by a space. If this happens when the user mistypes and enters something that is not an integer can you capture the error and simply ask him to repeat his input until you get 2 correct integer numbers?

Bonus points: Writing a parser is quite easy if the numbers are separated. It is quite a bit more tricky if you allow entering calculations with spaces between the operands and the operator. Can you write a parser accomplishing this?

Exercise 1:

Connect to your ESP32 with minicom or thonny.

Using REPL:

  • print “Hello World!”

  • read in a text using input() and print it

  • Calculate

  • Calculate sin(30°)
    If you see errors, how do you correct them? Are the results correct?

Exercise 2:

Write a script that assigns the values 5 and 3 to the variables a and b respectively

>
>
Improve this program asking the user to enter 2 real numbers (floats) separated by a space. This is how it should look like:
 
Changed:
<
<
Print the results of 4 basic arithmetic operations:
  • a+b

  • a-b

  • a*b

  • a/b

Which result do you observe for division? Can you correct?
>
>
calcOk.png
 
Changed:
<
<
Improve this program asking the user to enter 2 integers separated by a space. What happens when the user mistypes and enters something that is not an integer?
>
>
If this happens:
 
Changed:
<
<
Can you capture the error and simply ask him to repeat his input until you get 2 correct integer numbers?
>
>
conversionError.png
 
Changed:
<
<

Exercise 3:

>
>
when the user mistypes and enters something that is not an integer, can you capture the error and simply ask the user to repeat his input, until you get 2 correct integer numbers?
 
Changed:
<
<
Ask the user for 2 integer numbers. Check with one is bigger and print the result:
>
>
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?
 
Changed:
<
<
Running the program should result in output as shown below:
>
>
Try your parser on this input:
 
Added:
>
>
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
 

Exercise 3:

Changed:
<
<
Ask the user for 2 integer numbers. Check with one is bigger and print the result:
>
>
Like in exercise2, start with 2 number a,b with values 5 and 3. In your program check which of the 2 numbers is bigger and print the result. Then ask the user for 2 integer numbers. Make sure he enters 2 correct numbers and capture possible errors. This is a typical output:
 
Changed:
<
<
Running the program should result in output as shown below:
>
>
conditions.png
  -- Uli Raich - 2020-05-03

Comments

<--/commentPlugin-->
Added:
>
>
META FILEATTACHMENT attachment="conversionError.png" attr="" comment="" date="1588534969" name="conversionError.png" path="conversionError.png" size="19759" user="UliRaich" version="1"
META FILEATTACHMENT attachment="calcOk.png" attr="" comment="" date="1588535543" name="calcOk.png" path="calcOk.png" size="22212" user="UliRaich" version="1"
META FILEATTACHMENT attachment="conditions.png" attr="" comment="" date="1588537519" name="conditions.png" path="conditions.png" size="29528" user="UliRaich" version="1"

Revision 12020-05-03 - UliRaich

Line: 1 to 1
Added:
>
>
META TOPICPARENT name="Exercises"

Course on Internet of Things

Exercises Session 1:

Exercise 1:

Connect to your ESP32 with minicom or thonny.

Using REPL:

  • print “Hello World!”

  • read in a text using input() and print it

  • Calculate

    calculations.png

  • Calculate sin(30°)
    If you see errors, how do you correct them? Are the results correct?

Exercise 2:

Write a script that assigns the values 5 and 3 to the variables a and b respectively

Print the results of 4 basic arithmetic operations:

  • a+b

  • a-b

  • a*b

  • a/b

Which result do you observe for division? Can you correct?

Improve this program asking the user to enter 2 integers separated by a space. If this happens when the user mistypes and enters something that is not an integer can you capture the error and simply ask him to repeat his input until you get 2 correct integer numbers?

Bonus points: Writing a parser is quite easy if the numbers are separated. It is quite a bit more tricky if you allow entering calculations with spaces between the operands and the operator. Can you write a parser accomplishing this?

Exercise 1:

Connect to your ESP32 with minicom or thonny.

Using REPL:

  • print “Hello World!”

  • read in a text using input() and print it

  • Calculate

  • Calculate sin(30°)
    If you see errors, how do you correct them? Are the results correct?

Exercise 2:

Write a script that assigns the values 5 and 3 to the variables a and b respectively

Print the results of 4 basic arithmetic operations:

  • a+b

  • a-b

  • a*b

  • a/b

Which result do you observe for division? Can you correct?

Improve this program asking the user to enter 2 integers separated by a space. What happens when the user mistypes and enters something that is not an integer?

Can you capture the error and simply ask him to repeat his input until you get 2 correct integer numbers?

Exercise 3:

Ask the user for 2 integer numbers. Check with one is bigger and print the result:

Running the program should result in output as shown below:

Exercise 3:

Ask the user for 2 integer numbers. Check with one is bigger and print the result:

Running the program should result in output as shown below:

-- Uli Raich - 2020-05-03

Comments

<--/commentPlugin-->
 
This site is powered by the TWiki collaboration platform Powered by PerlCopyright © 2008-2024 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback