babybigfox
Posts: 15
Joined: Wed Jun 03, 2015 4:13 pm

Variables in list index numbers

Mon Jun 08, 2015 3:32 pm

Part of the code for my astro pi project contains a pickle which saves data about users. How would I specify an index number using a variable? Here's an example of my code

Code: Select all

data = [ A, B, C ]
person = 1
print(data[person])
This should print B.
Any help?
Thanks :D

scotty101
Posts: 3958
Joined: Fri Jun 08, 2012 6:03 pm

Re: Variables in list index numbers

Mon Jun 08, 2015 3:51 pm

With a tiny modification to you code, it now prints B. You were using the correct syntax for indexing in to a list but A, B and C had not been defined a printable items.

Code: Select all

data = [ 'A','B', 'C' ]
person = 1
print(data[person])
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

babybigfox
Posts: 15
Joined: Wed Jun 03, 2015 4:13 pm

Re: Variables in list index numbers

Mon Jun 08, 2015 4:03 pm

Thanks. What about:

Code: Select all

person = 1
usertemp = [ '0',  '1', '2' ]
ap.show_message('Temperature:' + usertemp[person])

scotty101
Posts: 3958
Joined: Fri Jun 08, 2012 6:03 pm

Re: Variables in list index numbers

Mon Jun 08, 2015 8:41 pm

That code should run ok unless there is something wrong with the ap.show_message function.
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

Return to “Astro Pi”