* balazs/python-calculator/calc.py:

This commit is contained in:
llnu 2020-06-01 16:55:04 +02:00
parent 4889241581
commit dd4140f687
5 changed files with 126 additions and 0 deletions

View file

@ -0,0 +1,22 @@
*** cdist #5: Generating Code & Exploring
**** Lecture content
***** Objective
- Understand how to *generate code* and when to use it
- Exploring explorers
***** Code generation steps
- Modify the previously created type *__my_nginx_site*
- Read about cdist messaging
- If there was a change in the nginx configuration file, reload nginx
***** Explorer steps
- Modify the *__my_nginx_site* type to use the *os explorer*
- Adjust your type to work on Alpine Linux and Debian (or two
other Linux distributions of your choice)
- Set the nginx configuration directory accordingly
***** Documentation
- Explain the following in your cdist.org file
- What is the difference between gencode-remote and the remote code?
- What is the difference between gencode-local and gencode-remote?
- Locate a type that comes with upstream cdist that uses
gencode-local - which one is it? Why does it need gencode-local?
**** Client notes

View file

@ -0,0 +1,60 @@
import sys
filename = sys.argv
def input_and_calculate_one_line():
printable_list = []
input_numbers = input("""Enter the numbers for summing (seperated by space), or enter "q" to exit.
""")
# print (input_numbers)
if input_numbers == "q":
return ""
else:
splitting_converting_summing_printing(input_numbers)
def splitting_converting_summing_printing(input_numbers):
split_numbers = input_numbers.split()
print (split_numbers)
converted_list = list(map(int, split_numbers))
print (converted_list)
summed_numbers = sum(converted_list)
print (summed_numbers)
printable_list = intersperse(converted_list, "+")
list_with_strings = convertListElementsToStr(printable_list)
converted_to_strings_list = listToString(list_with_strings)
print (converted_to_strings_list, "=", summed_numbers)
# for i in converted_list:
# printable_list.append(i)
# printable_list.append("+")
# printable_list += str(i)
# printable_list += '+'
def intersperse(lst, item):
result = [item] * (len(lst) * 2 - 1)
result[0::2] = lst
return result
def convertListElementsToStr(list):
# how_long = len
# result = []
result = map(str, list)
# result.append[str(i) for i in list]
return result
def listToString(s):
str1 = " "
return (str1.join(s))
input_and_calculate_one_line()

View file

@ -0,0 +1,44 @@
*** 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