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

(🐞 反爬虫测试!打击盗版⚠️)如果你看到这个信息, 说明这是一篇剽窃的文章,请访问 https://www.cnblogs.com/xgqfrms/ 查看原创文章!

refs



©xgqfrms 2012-2021

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

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


posted @ 2023-06-03 02:19  xgqfrms  阅读(10)  评论(1编辑  收藏  举报