xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

How to fix the for...in loop errors in Python All In One

How to fix the for...in loop errors in Python All In One

Python 3

errors ❌

TypeError: string indices must be integers

#!/usr/bin/env python3
# coding: utf8

# Pixel color order constants
RGB = "RGB"
"""Red Green Blue"""
GRB = "GRB"
"""Green Red Blue"""

# fix: 改成 tuple 元组 ❌
# RGBW = ("G", "R", "B", "W")
# TypeError: tuple indices must be integers or slices, not str ❌

RGBW = "RGBW"
"""Red Green Blue White"""
GRBW = "GRBW"
"""Green Red Blue White"""

class TestClass():
  # /Library/Python/3.9/site-packages/neopixel.py
  # *, 指定后面的参数都是 keyword 参数
  def __init__(self, *, bpp: int = 3, pixel_order: str = None):
    print("keyword 参数: bpp, pixel_order = ", bpp, pixel_order)
    print("✅ type = ", type(pixel_order))
    if not pixel_order:
      # Python 三元运算 / 三元运算符 ?:
      pixel_order = GRB if bpp == 3 else GRBW
    elif isinstance(pixel_order, tuple):
    # elif isinstance(pixel_order, str):
      # for...in, Python 遍历字符串的每一个字母
      # order_list = [RGBW[char] for char in pixel_order]
      # TypeError: string indices must be integers ❌
      # ❓ order 关键字
      order_list = [RGBW[order] for order in pixel_order]
      print("👻 order_list =", order_list)
      pixel_order = "".join(order_list)
      print("❓pixel_order =", pixel_order)
      print("\n")

# try:
#   test1 = TestClass(pixel_order="GRB")
#   test2 = TestClass(pixel_order="GRBW")
#   test3 = TestClass(bpp=3, pixel_order="GRB")
#   test4 = TestClass(bpp=4, pixel_order="GRBW")
# except KeyboardInterrupt:
#   print('Ctrl + C exit ✅')
# except RuntimeError as error:
#   print('error =', error, error.args[0])
#   pass
# except Exception as error:
#   print('exception =', error)
#   raise error
# finally:
#   # cleanup
#   print('finally, clear buffer! 👻')
# exit()

# $ py3 ./xyz.py

solutions

#!/usr/bin/env python3
# coding: utf8

import xyz
# from xyz import TestClass

# RGB = TestClass.RGB
# RGBW = TestClass.RGBW
# GRB =  TestClass.GRB
# GRBW = TestClass.GRBW
GRB =  xyz.GRB
GRBW = xyz.GRBW

# ❓ tuple
# 元组
# https://www.runoob.com/python3/python3-tuple.html
# tup1 = ("G", "R", "B", "W")
# tup2 = "G", "R", "B", "W"

# ✅ int index
tup1 = (0,1,2)
tup2 = (0,1,2,3)

# TypeError: string indices must be integers ❌
# https://stackoverflow.com/questions/6077675/why-am-i-seeing-typeerror-string-indices-must-be-integers
for order in tup1:
  print("❌ string index: order =", order, type(order))
# ❌ string index: order = G <class 'str'>
# ❌ string index: order = R <class 'str'>
# ❌ string index: order = B <class 'str'>
# ❌ string index: order = W <class 'str'>

try:
  test1 = xyz.TestClass(bpp=4, pixel_order=tup1)
  test2 = xyz.TestClass(bpp=4, pixel_order=tup2)

  # test1 = xyz.TestClass(pixel_order=GRB)
  # test2 = xyz.TestClass(pixel_order=GRBW)
  # test3 = xyz.TestClass(bpp=3, pixel_order=GRB)
  # test4 = xyz.TestClass(bpp=4, pixel_order=GRBW)

  # test1 = TestClass(pixel_order=GRB)
  # test2 = TestClass(pixel_order=GRBW)
  # test3 = TestClass(bpp=3, pixel_order=GRB)
  # test4 = TestClass(bpp=4, pixel_order=GRBW)

  # test1 = TestClass(pixel_order="GRB")
  # test2 = TestClass(pixel_order="GRBW")
  # test3 = TestClass(bpp=3, pixel_order="GRB")
  # test4 = TestClass(bpp=4, pixel_order="GRBW")
except KeyboardInterrupt:
  print('Ctrl + C exit ✅')
except RuntimeError as error:
  print('error =', error, error.args[0])
  pass
except Exception as error:
  print('exception =', error)
  raise error
finally:
  # cleanup
  print('finally, clear buffer! 👻')
exit()


# py3 ./test.py

image

demos

refs



©xgqfrms 2012-2021

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @   xgqfrms  阅读(11)  评论(1编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
历史上的今天:
2022-06-03 SwiftUI MapKit All In One
2021-06-03 ERR_NETWORK_CHANGED All In One
2021-06-03 十万个为什么-地铁是如何计算车厢拥挤度的?
2020-06-03 js function call hacker
2020-06-03 webpack & chunkhash
2020-06-03 React & Strict Mode
2019-06-03 lightning & web components & templates & slots
点击右上角即可分享
微信分享提示