Line: 1 to 1 | |||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Solutions to Exercise 1: REPL and standard Python programmingExercise 1: Use of REPLThe screen shot shows the REPL sessionExercise 2: A simple calculatorIn 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://iotworkshop.africa/pub/IoT_Course_English/SolutionsToREPLAndStandardPythonProgramming/assignments.py.txt | ||||||||||||||||||||||||||||||||||
> > | https://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://iotworkshop.africa/pub/IoT_Course_English/SolutionsToREPLAndStandardPythonProgramming/calculator.py.txt | ||||||||||||||||||||||||||||||||||
> > | https://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://iotworkshop.africa/pub/IoT_Course_English/SolutionsToREPLAndStandardPythonProgramming/calculatorV1.py.txt | ||||||||||||||||||||||||||||||||||
> > | https://iotworkshop.africa/pub/IoT_Course_English/SolutionsToREPLAndStandardPythonProgramming/calculatorV3.py.txt | ||||||||||||||||||||||||||||||||||
Exercise 3: ConditionsThere is nothing much new in this exercise except that it shows how to use conditional statements. The first version uses the fixed values 5 and 7: https://iotworkshop.africa/pub/IoT_Course_English/SolutionsToREPLAndStandardPythonProgramming/conditionsV1.py.txt The second version takes user input, splits it along spaces and makes sure that exactly 2 tokens have been given https://iotworkshop.africa/pub/IoT_Course_English/SolutionsToREPLAndStandardPythonProgramming/conditionsV2.py.txt The third version also checks that the 2 tokens are valid integer numbers https://iotworkshop.africa/pub/IoT_Course_English/SolutionsToREPLAndStandardPythonProgramming/conditionsV3.py.txtExercise 4: The Fibonacci SeriesIn this exercise we try to implement a mathematical function. This should allow you to write your own small algorithm and test it. The function you develop here will be used in exercise 6. Again there are 2 versions: The first one calculates the Fibonacci series F(n) where n is given by the user: https://iotworkshop.africa/pub/IoT_Course_English/SolutionsToREPLAndStandardPythonProgramming/fibonacciV1.py.txt while in the second version the user gives a maximum number not to be exceeded. https://iotworkshop.africa/pub/IoT_Course_English/SolutionsToREPLAndStandardPythonProgramming/fibonaciiV2.py.txtExercise 5: A bit of Mathematics: Calculate the sine functionHere we implement the calculation of the sine function we used with REPL in the first exercise. It shows how use external modules/libraries. This exercise is pretty simple. https://iotworkshop.africa/pub/IoT_Course_English/SolutionsToREPLAndStandardPythonProgramming/math.py.txtExercise 6: ClassesThis exercise is a bit bigger! It shows you how to develop your own modules and your own classes. In addition you may study some interesting mathematical number series. The last 4 series are actually approximations to well known mathematical constants:
Comments
| |||||||||||||||||||||||||||||||||||
Added: | |||||||||||||||||||||||||||||||||||
> > |
|