This is the repository for the online module Bots as Digital Infrapuncture, commissioned by the Utrecht University
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

106 lines
2.9 KiB

Title: Programming Logic
Slug: 03-s6-step-3
Date: 2020-11-01 12:02
Summary: Loops, if/else statements, variables and more.
<!-- > People, things, events are "programmed", one speaks of "inputs" and "outputs", of feedback loops, variables, parameters, processes, and so on, until eventually all contact with concrete situations is abstracted away.[^weizenbaum] -->
<!-- [^weizenbaum]: Weizenbaum, Joseph "Computer Power and Human Reason, From Judgement to Calculation" (1976) -->
As bots are written in code, they are based on the features and constraints of *programming logic*.
To unpack this term, we will look at how the following elements are operating, specifically in the programming language Python:
* data objects
* loops
* if/else statements
* variables
### Data Objects
How to store data?
Lists
```
agents = ['bot', programmer', 'user', 'moderator', 'server', 'regulator']
colors = ['red', 'magenta', 'yellow', 'blue', 'purple', 'green']
objects = ['apple', 'book', 'cable']
```
Dictionaries
```
vocabulary = {
'welcome' : 'Hello, how are you doing?',
'disagreement' : 'That makes no sense to me.',
'greeting' : 'Thanks for this, bye!'
}
```
### Loops
A loop is an element in programming that allows you to execute a single line of code multiple times. It is a central figure in automatising a task that is repetitive.
By using for example a `range()` loop, you can ask the program to execute a command `x` many times. In the following example, the `print()` command is asked to return the word `bot` as many times as the loop is iterating.
```
for x in range(5):
print('bot' * x)
> bot
> botbot
> botbotbot
> botbotbotbot
> botbotbotbotbot
```
Another type of loop is the `while` loop, which allows to repeat a command for an infinite number of times. This type of loop can be stopped by a manual interruption of the programmer, usually by typing a specific key-combination, such as `CTRL+D`.
```
while True:
print('bot')
> bot
> bot
> bot
> bot
> bot
```
Other loops, such as the `for` loop, are useful to iterate over a specific set of items. If you would like to write a bot that would, for example, post a message of each sentence of a book, you could loop over all the sentences and `print()` them one by one.
```
sentences = open('book.txt').readlines()
for sentence in sentences:
print('sentence')
> This is the first sentence.
> This is the second sentence.
> This is the third sentence.
```
### if/else statements
A next element in writing your bot could include the description of specific behavior at specific moments. If/else statements are used to trigger certain commands `if` a specific condition is met, or `else` execute an alternative command.
```
for x in range(5):
if x < 3:
print('Counting ... ' + str( x ))
else:
print('Nearing to the end ... ' + str( x ))
> Counting ... 0
> Counting ... 1
> Counting ... 2
> Nearing to the end ... 3
> Nearing to the end ... 4
```
### Variables
## Footnotes