AttributeError: 'float' - Objekt hat kein Attribut 'center'

Ich bin nur zu lernen, Python, und ich kann nicht herausfinden, wie zu lösen, ist ein Fehler. Der Python-code weiter unten schreibt einige Nachrichten auf dem Raspberry Pi 16x2-Bildschirm. Auf der 3. Satz von Nachrichten #AVG SPEED, wenn ich drucken print avgspeed es funktioniert einwandfrei, aber wenn ich die Ausgabe auf den Bildschirm bekomme ich diesen Fehler AttributeError: 'float' object has no attribute 'center'

import RPi.GPIO as GPIO
import time
from time import sleep
import socket
import fcntl
import struct
from pycgminer import CgminerAPI

LCD_RS = 25
LCD_E  = 24
LCD_D4 = 23 
LCD_D5 = 17
LCD_D6 = 18
LCD_D7 = 22

LCD_WIDTH = 16 
LCD_CHR = True
LCD_CMD = False

LCD_LINE_1 = 0x80
LCD_LINE_2 = 0xC0 

E_PULSE = 0.00005
E_DELAY = 0.00005

def main():

  GPIO.setmode(GPIO.BCM)       
  GPIO.setup(LCD_E, GPIO.OUT)  
  GPIO.setup(LCD_RS, GPIO.OUT) 
  GPIO.setup(LCD_D4, GPIO.OUT) 
  GPIO.setup(LCD_D5, GPIO.OUT) 
  GPIO.setup(LCD_D6, GPIO.OUT) 
  GPIO.setup(LCD_D7, GPIO.OUT) 

  lcd_init()

  #UNIT

  lcd_byte(LCD_LINE_1, LCD_CMD)
  lcd_string("UNIT 1",2)
  lcd_byte(LCD_LINE_2, LCD_CMD)
  lcd_string("MODEL BETA",2)
  sleep(5)

  #CLEAR

  lcd_byte(LCD_LINE_1, LCD_CMD)
  lcd_string("",2)
  lcd_byte(LCD_LINE_2, LCD_CMD)
  lcd_string("",2)
  sleep(0.4)

  #IP ADDRESS

  pi_ip = get_ip_address('eth0')
  lcd_byte(LCD_LINE_1, LCD_CMD)
  lcd_string("IP Address",2)
  lcd_byte(LCD_LINE_2, LCD_CMD)
  lcd_string(pi_ip,2)
  sleep(5)

  #CLEAR

  lcd_byte(LCD_LINE_1, LCD_CMD)
  lcd_string("",2)
  lcd_byte(LCD_LINE_2, LCD_CMD)
  lcd_string("",2)
  sleep(0.4)

  # AVG SPEED

  cgminer = CgminerAPI()
  summary = cgminer.summary()
  avgspeed = avg_speed()

  lcd_byte(LCD_LINE_1, LCD_CMD)
  lcd_string("Avg. Speed",2)
  lcd_byte(LCD_LINE_2, LCD_CMD)
  lcd_string(avgspeed,2)
  sleep(5)

  #CLEAR

  lcd_byte(LCD_LINE_1, LCD_CMD)
  lcd_string("",2)
  lcd_byte(LCD_LINE_2, LCD_CMD)
  lcd_string("",2)
  sleep(0.4)

def get_ip_address(ifname):
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    return socket.inet_ntoa(fcntl.ioctl(
        s.fileno(),
        0x8915,  # SIOCGIFADDR
        struct.pack('256s', ifname[:15])
    )[20:24])

def avg_speed():
    cgminer = CgminerAPI()
    summary = cgminer.summary()
    avg_speed_value = cgminer.command('summary')['SUMMARY'][0]['MHS av']
    return avg_speed_value

def lcd_init():
  lcd_byte(0x33,LCD_CMD)
  lcd_byte(0x32,LCD_CMD)
  lcd_byte(0x28,LCD_CMD)
  lcd_byte(0x0C,LCD_CMD)  
  lcd_byte(0x06,LCD_CMD)
  lcd_byte(0x01,LCD_CMD)  

def lcd_string(message,style):
  if style==1:
    message = message.ljust(LCD_WIDTH," ")  
  elif style==2:
    message = message.center(LCD_WIDTH," ")
  elif style==3:
    message = message.rjust(LCD_WIDTH," ")

  for i in range(LCD_WIDTH):
    lcd_byte(ord(message[i]),LCD_CHR)

def lcd_byte(bits, mode):
  GPIO.output(LCD_RS, mode) 
  GPIO.output(LCD_D4, False)
  GPIO.output(LCD_D5, False)
  GPIO.output(LCD_D6, False)
  GPIO.output(LCD_D7, False)
  if bits&0x10==0x10:
    GPIO.output(LCD_D4, True)
  if bits&0x20==0x20:
    GPIO.output(LCD_D5, True)
  if bits&0x40==0x40:
    GPIO.output(LCD_D6, True)
  if bits&0x80==0x80:
    GPIO.output(LCD_D7, True)

  time.sleep(E_DELAY)    
  GPIO.output(LCD_E, True)  
  time.sleep(E_PULSE)
  GPIO.output(LCD_E, False)  
  time.sleep(E_DELAY)      

  GPIO.output(LCD_D4, False)
  GPIO.output(LCD_D5, False)
  GPIO.output(LCD_D6, False)
  GPIO.output(LCD_D7, False)
  if bits&0x01==0x01:
    GPIO.output(LCD_D4, True)
  if bits&0x02==0x02:
    GPIO.output(LCD_D5, True)
  if bits&0x04==0x04:
    GPIO.output(LCD_D6, True)
  if bits&0x08==0x08:
    GPIO.output(LCD_D7, True)

  time.sleep(E_DELAY)    
  GPIO.output(LCD_E, True)  
  time.sleep(E_PULSE)
  GPIO.output(LCD_E, False)  
  time.sleep(E_DELAY)   

if __name__ == '__main__':
  main()

Traceback:

Traceback (most recent call last):
  File "screen.py", line 165, in <module>
    main()
  File "screen.py", line 81, in main
    lcd_string(avgspeed,2)
  File "screen.py", line 117, in lcd_string
    message = message.center(LCD_WIDTH," ")
AttributeError: 'float' object has no attribute 'center'

Kann mir jemand helfen mit diesem?

Dank

Poste bitte die ganze Ausnahme-und traceback-statt nur versuchen, zu beschreiben, wo der Fehler passiert.
Post geändert mit traceback Antwort. Dank

InformationsquelleAutor Andrei | 2013-12-20

Schreibe einen Kommentar