this is the error I am gtting.

    webide@raspberrypi /usr/share/adafruit/webide/repositories/my-pi-projects/temp4 $ sudo python temp4.py                        
  86/1023 => 0.277 V => 27.7 °C                                                                                               
  86/1023 => 0.277 V => 81.9 °F                                                                                               
Traceback (most recent call last):                                                                                            
  File "temp4.py", line 55, in <module>                                                                                       
    display_temp()                                                                                                            
  File "temp4.py", line 25, in display_temp                                                                                   
    temp = int(temperaturef [0]) # F                                                                                          
TypeError: 'float' object has no attribute '__getitem__'                                                                      
webide@raspberrypi /usr/share/adafruit/webide/repositories/my-pi-projects/temp4 $ 

this is my code,

#!/usr/bin/python
# -*- coding: utf-8 -*-
# mcp3008_lm35.py - read an LM35 on CH0 of an MCP3008 on a Raspberry Pi
# mostly nicked from
#  http://jeremyblythe.blogspot.ca/2012/09/raspberry-pi-hardware-spi-analog-inputs.html

import spidev
import time
from Adafruit_7Segment import SevenSegment
segment = SevenSegment(address=0x70)

spi = spidev.SpiDev()
spi.open(0, 0)

def readadc(adcnum):
# read SPI data from MCP3008 chip, 8 possible adc's (0 thru 7)
    if adcnum > 7 or adcnum < 0:
        return -1
    r = spi.xfer2([1, 8 + adcnum << 4, 0])
    adcout = ((r[1] & 3) << 8) + r[2]
    return adcout

def display_temp():
    segment.setColon(False)
    temp = int(temperaturef [0]) # F
    # temp = int(read_temp()[0]) # C
    sign = (temp < 0)
    temp = abs(temp)
    digit_1 = temp % 10
    temp = temp / 10
    digit_2 = temp % 10
    temp = temp / 10
    digit_3 = temp % 10
    if sign :
        segment.writeDigitRaw(0, 0x40)       # - sign
    if digit_3 > 0 :
        segment.writeDigit(0, digit_3)       # Hundreds
    else:
        segment.writeDigitRaw(0, 0)
    if digit_2 > 0 :
        segment.writeDigit(1, digit_2)       # Tens
    else:
        segment.writeDigitRaw(1, 0)
        segment.writeDigit(3, digit_1)           # Ones
        segment.writeDigitRaw(4, 0x71) #F        # Temp units letter
        #segment.writeDigitRaw(4, 0x39) #C

while True:
    value = readadc(0)
    volts = (value * 3.3) / 1024
    temperaturec = volts / (10.0 / 1000)
    temperaturef = ( temperaturec * 9.0 / 5.0 ) + 32
    print ("%4d/1023 => %5.3f V => %4.1f °C" % (value, volts,temperaturec))
    print ("%4d/1023 => %5.3f V => %4.1f °F" % (value, volts,temperaturef))
    display_temp()
    time.sleep(0.5) 

OK, I'm puzzled about that myself, but not for the same reason. At this point in the function, the variable temperaturef doesn't have any set value, which means it should be giving a NameError exception instead, like this:

NameError: name 'temperaturef' is not defined

Instead, it is acting as if the temperaturef used in the top-level code has already been set to a float value, as it is on line 52. This shouldn't be in scope inside of display_temp().

Now, if we assume that temperaturef is in scope for some reason, then it makes sense. The __getitem__() special method is the function used in collection classes to overload index operator '[]'. By having the [0] folliwing temperaturef, you are in effect trying to treat the variable as if it were a collection variable, which it isn't.

Instead, it is acting as if the temperaturef used in the top-level code has already been set to a float value, as it is on line 52.

It is on line 52, but since line 52 runs before the display_temp function is called on line 55, the variable has indeed already been set when line 25 executes.

This shouldn't be in scope inside of display_temp().

Variables set at the top level are global and thus always in scope unless they're being shadowed by a local variable.

i am looking to make these changes to the code i frist put up. I will try this later today.
i know these code will not look right, but i am getting things worked out for now.

display_temp():

    segment.setColon(False)



    temp = temp_f # F



    # temp = int(read_temp()[0]) # C

    sign = (temp < 0)
    temp = abs(temp)

    digit_1 = temp % 10
    temp = temp / 10

    digit_2 = temp % 10
    temp = temp / 10

    digit_3 = temp % 10
    if sign :

    segment.writeDigitRaw(0, 0x40)       # - sign


    if digit_3 > 0 :

        segment.writeDigit(0, digit_3)       # Hundreds

    else:

          segment.writeDigitRaw(0, 0)

    if digit_2 > 0 :

        segment.writeDigit(1, digit_2)       # Tens

    else:

        segment.writeDigitRaw(1, 0)

        segment.writeDigit(3, digit_1)           # Ones

            segment.writeDigitRaw(4, 0x71) #F        # Temp units letter

        #segment.writeDigitRaw(4, 0x39) #C




while True:

    value = readadc(0)
    volts = (value * 3.3) / 1024

    temperaturec = volts / (10.0 / 1000)

    temperaturef = ( temperaturec * 9.0 / 5.0 ) + 32


    temp_f= int(math.floor(temperaturef))

    print ("%4d/1023 => %5.3f V => %4.1f °C" % (value, volts,temperaturec))

    print ("%4d/1023 => %5.3f V => %4.1f °F" % (value, volts,temperaturef))

    display_temp()

    time.sleep(0.5) 
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.