Difference: SolutionsToREPLAndStandardPythonProgramming (6 vs. 7)

Revision 72020-05-29 - UliRaich

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

Solutions to Exercise 1: REPL and standard Python programming

Exercise 1: Use of REPL

Line: 10 to 10
  In the first example we use fixed values for the calculations. To print out the results I first use integer (%d) format and then floating point format (%f) to get the division right.
Changed:
<
<
https://afnog.iotworkshop.africa/pub/IoT_Course_English/SolutionsToREPLAndStandardPythonProgramming/assignments.py.txt
>
>
https://afnog.iotworkshop.africa/pub/IoT_Course_English/SolutionsToREPLAndStandardPythonProgramming/calculatorV1.py.txt
  In the second example I define 4 functions for the 4 basic arithmetic operations. The dictionary with the operator as key and the function as value allows me to select the function to be called depending on the operator given. I write a simple parser to check that user input is correct. Since I force a space between operands and operator it becomes easy to split the calculation into 3 tokens: 2 operands and the operator. Now all I need to do is converting the operator strings to floating values and call the corresponding function.
Changed:
<
<
https://afnog.iotworkshop.africa/pub/IoT_Course_English/SolutionsToREPLAndStandardPythonProgramming/calculator.py.txt
>
>
https://afnog.iotworkshop.africa/pub/IoT_Course_English/SolutionsToREPLAndStandardPythonProgramming/calculatorV2.py.txt
  The last example is quite a bit more tricky as I cannot use the string method split to separate operands and operator any more. The parser allows the characters 0..9,"." and "+","-","*","/".

The operator may only consist of 1 or more digits and/or the decimal point. It may not contain more than 1 decimal point and it must contain at least 1 digit. The operator can be only one of the above basic arithmetic operators. Here is my solution. Test it yourself and try to find an illegal character combination that is not treated. Error handling is often the most difficult part of a program!

Changed:
<
<
https://afnog.iotworkshop.africa/pub/IoT_Course_English/SolutionsToREPLAndStandardPythonProgramming/calculatorV1.py.txt
>
>
https://afnog.iotworkshop.africa/pub/IoT_Course_English/SolutionsToREPLAndStandardPythonProgramming/calculatorV3.py.txt
 

Exercise 3: Conditions

There is nothing much new in this exercise except that it shows how to use conditional statements.

Line: 118 to 118
 
META FILEATTACHMENT attachment="mathSeries.py.txt" attr="" comment="" date="1590670253" name="mathSeries.py.txt" path="mathSeries.py.txt" size="3484" user="UliRaich" version="1"
META FILEATTACHMENT attachment="mathSeriesPlot.py.txt" attr="" comment="" date="1590670253" name="mathSeriesPlot.py.txt" path="mathSeriesPlot.py.txt" size="3233" user="UliRaich" version="1"
META FILEATTACHMENT attachment="mathSeriesTest.py.txt" attr="" comment="" date="1590670253" name="mathSeriesTest.py.txt" path="mathSeriesTest.py.txt" size="2920" user="UliRaich" version="1"
Added:
>
>
META FILEATTACHMENT attachment="calculatorV3.py.txt" attr="" comment="" date="1590768475" name="calculatorV3.py.txt" path="calculatorV3.py.txt" size="1206" user="UliRaich" version="1"
 
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