【发布时间】:2019-05-08 13:41:07
【问题描述】:
我遇到了一个尴尬的问题。
TypeError: ord() expected a character, but string of length 2 found
我的代码:
for c in input_s:
if ord('a') <= ord(c.lower()) <= ord('z'):
e_count += 1
我预计 for c in input_s 是 string 和 c always 字符被分配。这种情况很少发生,因此很难重新产生问题。 c 变量是否有长度为 2 的字符串?
【问题讨论】:
-
请向我们展示
input_s列表... -
@StephenRauch
e_count = sum('a' <= c.lower() <= 'z' for c in input_s)(但仍然没有回答问题。) -
@DYZ,实际上你可能会回答这个问题,因为它会消除 str len 2 错误。
-
@StephenRauch 它可能会隐藏问题但不能解决问题。为什么字符串的一个元素会是一个有多个字符的字符串?
-
@hygull 在 python3 中没有这样的问题,Keanu 可以分享一下你是如何创建
input_s的,问题与它无关。这是一个类型不匹配的问题
标签: python python-3.x