Python Forum

Full Version: Problem with variables
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
>Hi All,
I am trying to build a function to write text to a 20X4 LCD.
I have imported the appropriate driver and defined the name of the lcd.
lcdBB=I2C_LCD_driver_twoLCD.lcd(0x26)
Now i want to use a variable that is given as a parameter in the def of the function: side.
Side can be BB (port) or SB (starbord).
i now do:
dspy='lcd'+side
which results in:
>>> dspy
'lcdSB'
To write a line to the lcd i do:
>>> lcdBB.lcd_display_string(boord+' pomp OFF',4)
that works fine so i thought, can i construct the 'lcdBB" from the parameters of the function and thus i did:
>>> dspy.lcd_display_string(boord+' pomp OFF',4)
Error:
Traceback (most recent call last): File "<pyshell>", line 1, in <module> AttributeError: 'str' object has no attribute 'lcd_display_string'
With the above AttributeError as result.
Apparently i can not substitute the pre-defined 'lcdBB' by the constructed string dspy=('lcd'+side).

Can anyone advise if there is a way to do what i want (if possible at all).

Rgds Steffen
which package do you use?
please post the whole code in python tags.
Hi Buran,
Thanks for your reaction.
I dont have a complete program -if thats what you mean by package- yet.
If by package you refer to driver, its the published
import I2C_LCD_driver.py
modified to
!2C_LCD_driver_twolcd.py
to accept a parameter as the address of the lcd (0x26 or 0x27).
I thought it best to get the individual components working first.
Then i will build the function i am looking for.
No use building a house when you don't know if the bricks are OK Smile

By the way Buran,
i received a notice that you fixed the error tags.
Apparently i have done something wrong or made an error i am not aware of.
Could you please tel what i did wrong?

greetings Steffen
(Jul-14-2018, 08:28 AM)Steffenwolt Wrote: [ -> ]By the way Buran,
i received a notice that you fixed the error tags.
Apparently i have done something wrong or made an error i am not aware of.
Could you please tel what i did wrong?
In the original post the traceback was in python tags. I changed these to error tags, so it now looks like error not code.

As to the rest - I'm not sure I understand what you are doing/what have you done..
I'll start a new topic.
I think i lost my train of thoughts because i am not sure what it was i was truing to do.

Tks sofar, Steffen