Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python def not changing values of integers

I made a def which is supposed to add 1 to an integer, but it doesn’t.

import ssd1306
import machine
from machine import Pin
import time
import uos
import machine

ez = 0
r = 0
e = 8
nah = 10
erg = 10

WIDTH = 128
HEIGHT = 64
i2c = machine.I2C(0)

def ereter(ez, r, e):
    ez+=1
    print(ez)
    oled.fill(0)
    oled.text("{}".format(ez), 35, 10)
    oled.text(":{}".format(r), 50, 10)
    oled.text(":{}".format(e), 72, 10)
    oled.text("The time is ^ rn", 0, 30)
    oled.text("i2c devices:"+str(i2c.scan()), 0, 40)
    oled.show()
    if ez>58: r+=1
    if ez>58: ez-=60
    if r>58: e+=1
    if r>58: r-=60
    if e>11: e-=12

while nah>1:
    ereter(ez, r, e)
    time.sleep(1)

I was expecting the code to add 1 to ez every second, but instead, it just doesn’t change the value of ez.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You know inside functions all global variables (variables that is defined above after modules) are rewrited. To solve this problem maybe you should add global key after def key. It should look like this:

def ereter(ez, r, e):
    global ez
    ez+=1
    # continue of function
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading