44 lines
1.9 KiB
Org Mode
44 lines
1.9 KiB
Org Mode
*** Python applying learnings from 1..6
|
|
**** Lecture notes
|
|
- Previous topics covered:
|
|
- Printing
|
|
- Formatting
|
|
- Variables
|
|
- Escape Sequences
|
|
- Inputting text
|
|
- Reading arguments / using argv
|
|
- Reading files
|
|
- Defining methods
|
|
- Boolean logic
|
|
- Branching using if/else/elif
|
|
- Loops: for/while
|
|
- Today we write a calculator that saves results in a file in python
|
|
- How it works in general
|
|
You read the input until you read a line that only contains a
|
|
"q". Every input line consists of numbers separated by a
|
|
space. For instance "4 5 9". You will need to .split() the
|
|
input.
|
|
- Steps
|
|
- Create a python script named "calc.py"
|
|
- It takes 1 command line argument (argv), which is the filename
|
|
- We will store the calculations *and* results in this file
|
|
- Create a method named "input_and_calculate_one_line"
|
|
- It does not have any arguments
|
|
- It reads one line via *input*
|
|
- It splits the input (let's say "4 5 9" => [ "4", "5", "9") ])
|
|
- It calculates the result (f.i. 4+5+9 = 18) and stores it in
|
|
a variable (use *sum* over the *list*)
|
|
- It returns a string of the format "4 + 5 + 9 = 18"
|
|
- If the line only contains a "q" it return "" (an empty string)
|
|
- Create a method named "editor" that takes a filename as an argument
|
|
- It opens the file for writing
|
|
- It uses input_and_calculate_one_line in a while loop
|
|
- while the return result is not "", we append the string to
|
|
the file
|
|
- When the return result is "", the function exits
|
|
|
|
*** State:
|
|
input_and_calculate_one_line is complete, however it's probably a garbage implementation.
|
|
Spent too much time on converting back and forth and getting the output right.
|
|
|
|
Editor (file io) is not complete yet
|