User avatar
NPDedyukhin
Posts: 29
Joined: Fri Sep 20, 2019 3:23 am
Location: RU

i2c_lcd and python. Display of variable value.

Wed Oct 09, 2019 1:49 pm

Good afternoon!

There is a module "i2c_lcd" and python.

At the beginning of the script, I assign the variable "a" to 1 (am I doing the assignment correctly?)

Next, I try to display the value of the variable on the display.

I wrote a script like this:

Code: Select all

#!/usr/bin/env python
# -*- coding: utf-8 -*-
a = 1

import lcddriver

lcd = lcddriver.lcd()

lcd.lcd_clear()
lcd.lcd_display_string(a, 1)
Apparently, I'm doing something wrong, because after running this script, nothing is displayed.

Writes an error: TypeError: 'int' object is not iterable

Please help me figure out what the problem is.
Respectfully,
Nikita Dedyukhin

User avatar
B.Goode
Posts: 9228
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: i2c_lcd and python. Display of variable value.

Wed Oct 09, 2019 1:58 pm

At the beginning of the script, I assign the variable "a" to 1 (am I doing the assignment correctly?)

Code: Select all

a = 1
That assigns the numeric value '1' to the variable named a.


Code: Select all

lcd.lcd_display_string(a, 1)
It seems reasonable to guess that lcd_display_string() will output a string on the display. (Reading the documentation for the module you are using is recommended.)

But as discussed above your variable a contains a number, not a string.

User avatar
NPDedyukhin
Posts: 29
Joined: Fri Sep 20, 2019 3:23 am
Location: RU

Re: i2c_lcd and python. Display of variable value.

Wed Oct 09, 2019 1:59 pm

NPDedyukhin wrote:
Wed Oct 09, 2019 1:49 pm
Good afternoon!

There is a module "i2c_lcd" and python.

At the beginning of the script, I assign the variable "a" to 1 (am I doing the assignment correctly?)

Next, I try to display the value of the variable on the display.

I wrote a script like this:

Code: Select all

#!/usr/bin/env python
# -*- coding: utf-8 -*-
a = 1

import lcddriver

lcd = lcddriver.lcd()

lcd.lcd_clear()
lcd.lcd_display_string(a, 1)
Apparently, I'm doing something wrong, because after running this script, nothing is displayed.

Writes an error: TypeError: 'int' object is not iterable

Please help me figure out what the problem is.
I rewrote the code and it worked:

Code: Select all

#!/usr/bin/env python
# -*- coding: utf-8 -*-
a = 1
a = str(a)

import lcddriver

lcd = lcddriver.lcd()

lcd.lcd_clear()
lcd.lcd_display_string(a, 1)
It was necessary to set the variable type "str"
Respectfully,
Nikita Dedyukhin

Andyroo

Re: i2c_lcd and python. Display of variable value.

Wed Oct 09, 2019 3:29 pm

NPDedyukhin wrote:
Wed Oct 09, 2019 1:59 pm
...

I rewrote the code and it worked:

Code: Select all

#!/usr/bin/env python
# -*- coding: utf-8 -*-
a = 1
a = str(a)

import lcddriver

lcd = lcddriver.lcd()

lcd.lcd_clear()
lcd.lcd_display_string(a, 1)
It was necessary to set the variable type "str"
A simpler method would be to set the value as a string first:

Code: Select all

#!/usr/bin/env python
# -*- coding: utf-8 -*-
a = "1"

import lcddriver

lcd = lcddriver.lcd()

lcd.lcd_clear()
lcd.lcd_display_string(a, 1)
You can also use the single quote ( ' ) to denote a string. If you are using some editors (or iPads etc) you have to be careful as the will try to correct the quotes to match correct English so "test" becomes “test” and the program fails...

Have a look at https://www.tutorialspoint.com/python/p ... _types.htm for some basic information on types of variables.

Return to “Python”