---
# System prepended metadata

title: Week III

---

# Week III

## Review: Magic 8-Ball
- [ ] Library use
- [ ] Random

## I: Dog Years
- [ ] Conversion
    - [ ] Main


```
function(variable)
tmp_1=variable
tmp_2=variable <- goes to function space

```

# Stroop Effect
## What do we want to do?
- [ ] Time people on their responses
    - [ ] Control
        - [ ] Cycle through pairs (pink,pink) (blue,blue)
    - [ ] Experiment
        - [ ] Cycle through COLORS and a random label (pink,*)
            - [ ] Prevent repitition
## What do we need?
- [ ] Timer
- [x] Colors
- [x] Random
- [ ] LOOP
    - [ ] For -> start_time + 10, then stop
        - [ ] X=start_time+10
    - [ ] While
        - [ ] time.now-start_time < 10
- [ ] IF

- Time thing which gives the number of seconds since epoch `help(function)`

## Analysis of Boilerplate

```python
import random
import time
from colorama import init # Same as using colorama.init()
from termcolor import colored
init(autoreset=True)
```

## Lists
- Can be heterogenous
- Use `type` when in doubt
- Goes from 0 to N-1 (for a list of N elements)
- Access with `X[N]`

## Random Choices

```python
x[random.randint(0,len(x)-1)] 
# is the same as
random.choice(x)
```

## Given
- [ ] Something to print with
- [ ] Something to choose strings out of

# Solution

```python
import random
import time
from colorama import init
from termcolor import colored
init(autoreset=True) # actually is colorama.init

MAX_TIME=2

def main():
    print_intro()
    start_time=time.time()
    correct=0
    incorrect=0
    while time.time() - start_time < MAX_TIME:
        a=random_color_name()
        b=random_color_name()
        print_in_color(a,b)
        ans=(a==b)
        trial=input("Press a key based on the instructions: ")
        if trial=='t' and ans:
            print(ans)
            correct=correct+1
        elif trial!='t' and not ans:
            print(ans)
            correct=correct+1
        else:
            print(ans)
            incorrect=incorrect+1
    print("You got ",correct," correct answers")
    print("You got ",incorrect, "incorrect answers")
    # Your solution goes here

def print_intro():
    '''
    Function: print intro
    Prints a simple welcome message
    '''
    print('This is the Stroop test! Name the font-color used:')
    print_in_color('red', 'red')
    print_in_color('blue', 'blue')
    print_in_color('pink', 'pink')
    print("If the color is the same as the meaning of the word, press t")
    

def random_color_name():
    '''
    Function: random color
    Returns a string (either red, blue or pink) with equal likelihood
    '''
    return random.choice(['red', 'blue', 'pink'])

def print_in_color(text, font_color):
    '''
    Function: print in color
    Just like "print" but this time, you can specify the font-color
    '''
    if font_color == 'pink': # magenta is a lot to type...
        font_color = 'magenta'
    print(colored(text, font_color, attrs=['bold']))


if __name__ == '__main__':
    main()
```