# Python beginners question

**URL:** <https://forum.critterandguitari.com/t/python-beginners-question/2182>\
**Category:** ETC\
**Created:** [December 23, 2017, 8:24pm UTC](https://forum.critterandguitari.com/t/python-beginners-question/2182 "2017-12-23T20:24:41Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![fanwander](https://yyz1.discourse-cdn.com/flex035/user_avatar/forum.critterandguitari.com/fanwander/32/5076_2.png) [@fanwander](https://forum.critterandguitari.com/u/fanwander)\
**Post date:** [December 23, 2017, 8:24pm UTC](https://forum.critterandguitari.com/t/python-beginners-question/2182/1 "2017-12-23T20:24:41Z")

</div>

Hello,

I am trying to start writing own modes. I am used to program shell scripts, and I can mostly understand and sometimes modify Perl, Java, Groovy, or even C++ code. But I don’t have any Python experience until now.

So I tried a small example: let a line move from left to right. The color and the position are changed by the same variable. That works fine with this script:  
-------------snip------------  
import os  
import pygame  
import math

```
    count1 = 0

    def setup(screen, etc):
        pass

    def draw(screen, etc):
        global count1

        if count1 > 254 :
           count1 = 0
        count1 = int(count1 + 1)
        
        color = count1,0,0
        xpos = int(count1*5)
        thick = 100
        pygame.draw.line(screen, color, [xpos, 1], [xpos, 720], thick)
-------------snip------------

```

Now I wanted to move forward and backward. I use this code:

```
-------------snip------------
 import os
 import pygame
 import math

 count1 = 0
 direction = 0

 def setup(screen, etc):
    pass

 def draw(screen, etc):
    global count1
    global direction
    
    
    if count1 > 254 :
        direction = 1
    if count1 < 1 :
        direction = 0

    if direction = 0 :
        count1 = int(count1 + 1)
    if direction = 1 :
        count1 = int(count1 - 1)
    

    color = count1,0,0
    xpos = int(count1*5)
    thick = 100
    pygame.draw.line(screen, color, [xpos, 1], [xpos, 720], thick)

-------------snip------------

```

But this time the mode won’t load at all.

What am I doing wrong here?

Any help is appreciated.

---

<div class="post-metadata">

**Author:** ![thetechnobear](https://yyz1.discourse-cdn.com/flex035/user_avatar/forum.critterandguitari.com/thetechnobear/32/42_2.png) [@thetechnobear](https://forum.critterandguitari.com/u/thetechnobear)\
**Post date:** [December 23, 2017, 9:23pm UTC](https://forum.critterandguitari.com/t/python-beginners-question/2182/2 "2017-12-23T21:23:14Z")

</div>

its == , not = (like c++) for equals.  
see  
[https://www.tutorialspoint.com/python/python\_basic\_operators.htm](https://www.tutorialspoint.com/python/python_basic_operators.htm)

tip: if you press the OSD button, often it will show the error , including line number

---

<div class="post-metadata">

**Author:** ![fanwander](https://yyz1.discourse-cdn.com/flex035/user_avatar/forum.critterandguitari.com/fanwander/32/5076_2.png) [@fanwander](https://forum.critterandguitari.com/u/fanwander)\
**Post date:** [December 23, 2017, 9:52pm UTC](https://forum.critterandguitari.com/t/python-beginners-question/2182/3 "2017-12-23T21:52:30Z")

</div>

Oh, yes. Silly me. Thanks!

The OSD view didn’t help in this case as the Mode didn’t load at all (that was what the OSD view did tell…)

---

<div class="post-metadata">

**Author:** ![fanwander](https://yyz1.discourse-cdn.com/flex035/user_avatar/forum.critterandguitari.com/fanwander/32/5076_2.png) [@fanwander](https://forum.critterandguitari.com/u/fanwander)\
**Post date:** [December 29, 2017, 10:48am UTC](https://forum.critterandguitari.com/t/python-beginners-question/2182/4 "2017-12-29T10:48:51Z")

</div>

Hello again,

now some other stupid question which a real programmer would never ask 🙂 …

I use a lot of code, which is repeating in each mode. I’d like to have this in central library file and include it in my different modes [main.py](http://main.py). For example I have a simple “toggle switch” that switches at each trigger, I have a kind of LFO, which counts from 0 to 255 and back. Also I have a monitoring output, which displays the values of some variables as numbers on the screen.

Until now I hold those in a text file and copy paste them. But each time I improve such a standard routine, I have to add this improvement to all my modes. That is annoying.

So is there some way to use something like `MyLIbrary.LFO` similar like the `etc.knobX`?

Example:  
`MyLibrary.LFO(number,speed)` gets an integer parameter for the selection of LFO 1 to 4 and gets an integer for the speed value (not discussing here how this speed setting is achieved). It returns an integer which changes at the given speed  
So I could set  
`color = ( MyLibrary.LFO(1,10), MyLibrary.LFO(2,11),MyLibrary.LFO(3,12) )`

Some of these functions might be tricky as they depend on global variables; can this be handled?

Am I missing other pit falls?

Best regards  
Florian

---

<div class="post-metadata">

**Author:** ![thetechnobear](https://yyz1.discourse-cdn.com/flex035/user_avatar/forum.critterandguitari.com/thetechnobear/32/42_2.png) [@thetechnobear](https://forum.critterandguitari.com/u/thetechnobear)\
**Post date:** [December 29, 2017, 11:55am UTC](https://forum.critterandguitari.com/t/python-beginners-question/2182/5 "2017-12-29T11:55:45Z")

</div>

lookup python modules, variables and functions are scoped to the module.

then for more structure you could look at classes.

---

<div class="post-metadata">

**Author:** ![fanwander](https://yyz1.discourse-cdn.com/flex035/user_avatar/forum.critterandguitari.com/fanwander/32/5076_2.png) [@fanwander](https://forum.critterandguitari.com/u/fanwander)\
**Post date:** [December 29, 2017, 3:12pm UTC](https://forum.critterandguitari.com/t/python-beginners-question/2182/6 "2017-12-29T15:12:55Z")

</div>

Thank you again!
