Course on Internet of Things

Exercises Session 1:

Introductory remarks:

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

Connect to your ESP32 with minicom or thonny. You may try the same thing running Python3 on your PC.

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?

  • 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

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 real numbers (floats) separated by a space. This is how it should look like:

calcOk.png

If this happens:

conversionError.png

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?

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:

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
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

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.

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

Comments

Topic attachments
I Attachment History Action Size Date Who Comment
PNGpng calcOk.png r1 manage 21.7 K 2020-05-03 - 19:52 UliRaich  
PNGpng conditions.png r1 manage 28.8 K 2020-05-03 - 20:25 UliRaich  
PNGpng conversionError.png r1 manage 19.3 K 2020-05-03 - 19:42 UliRaich  
PNGpng e.png r1 manage 1.9 K 2020-05-04 - 12:56 UliRaich  
PNGpng factorial.png r1 manage 2.8 K 2020-05-04 - 12:48 UliRaich  
PNGpng geometric.png r1 manage 3.9 K 2020-05-04 - 12:48 UliRaich  
PNGpng harmonic.png r1 manage 1.6 K 2020-05-04 - 12:53 UliRaich  
PNGpng ln2.png r1 manage 2.0 K 2020-05-04 - 12:48 UliRaich  
PNGpng pi.png r1 manage 2.7 K 2020-05-04 - 12:48 UliRaich  
Edit | Attach | Watch | Print version | History: r17 | r7 < r6 < r5 < r4 | Backlinks | Raw View | Raw edit | More topic actions...
Topic revision: r5 - 2020-05-04 - UliRaich
 
  • Edit
  • Attach
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