Difference: REPLAndStandardPythonProgramming (1 vs. 2)

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