Python recap
Previous: Programme | Next: The path and simple file reading |
Required course material for the lesson
Powerpoint: Python Recap
Video: Playing the guessing game, strategies and advice.
Resource: Example code - Basics
If you feel your basic python skills are lacking, then there is a discontinued - but available - softer introduction course: 22101
Powerpoint: Python basic Specially recommended for people lacking python experience
Subjects covered
This is a superficial recap on Python variables, assignment, some operators (+-*/.), conditional statements with if, looping using for and while. built-in functions like print, len, range, input and type casting functions int, float and str. Well-known list methods like max, min and sum. Various standard libraries like os, sys, and math.
It is expected that students know python to this level when starting the course.
Advice
Data and program code are separate entities, which go together. However, data can not be executed as code, and code is not typically thought of as data.
You can assume that the user is well-behaved enough to only input numbers, when numbers are required.
You can not assume that the user will not input numbers which in some way will give rise to a wrong/faulty/misbehaving calculation.
Exercises to be handed in
A new function to be used in these exercises are the input function which gets a string as input from the keyboard.
Hint: use a list in any exercise and you fail.
- First a tricky exercise - read carefully and think what this means. Make a program that asks for number, and then continues asking for numbers as long as you input numbers that are greater or equal to all previous numbers (not the sum of previous numbers).
- You have previously calculated the factorial of a number - this is somewhat similar. Ask for an integer and calculate the sum from 0 up/down to the integer.
Example: If you input 5 then calculate 5 + 4 + 3 + 2 + 1 (+ 0) = 15. If input is -4 then calculate -4 + -3 + -2 + -1 (+ 0) = -10. - Ask for a word 5 times. Display all the words afterwards on one line.
- In a loop ask for words. Add them together in the order given so they form a (maybe meaningless) sentence. When you do not enter anything (i.e. just hit the <enter> key), you stop the loop and display the sentence.
- In a loop ask for numbers using the input function. Add the numbers together as the are entered. End the loop when you write STOP instead of a number. Display the sum of the numbers.
Hint: Check for STOP before you try to typecast (convert) the number. - Continue to ask for numbers until you don't enter anything. Then display all the numbers and their sum.
Example: The numbers 1, 2 & 3 is entered and "1 + 2 + 3 = 6" is displayed.
Hint: You need use the numbers both as string type and number type. - Ask for 2 integers. Now print the first 20 numbers of the Fibonacci sequence given by those numbers.
If the input is 1 & 3, the output would be 1 3 4 7 11 18 29 47 etc.. The next number is the sum of the 2 previous. - Now for some playing. Make a program that guesses a number between 1 and 10 that you think of. It should make a guess, and you shall answer yes (if correctly guessed), higher (if the number you think of is higher than the guess) or lower (if the number you think of is lower than the guess). The program ends when the number is guessed correctly, otherwise it tries again. It is NOT considered OK to guess at a number more than once, i.e. no repeats. It is counterproductive to try to make this random guessing. Also think about how simple you can make this and still fulfill the wording of the exercises. Just don't run with the first idea you get. Don't embellish with additional functionality - it will just get difficult and we save that for later. Just to be clear - the number you think of must nowhere be in the program.