python - 如何修复返回 TypeError 的 len() 命令?
问题描述
我正在尝试在 python 中计算字符串的长度,但是我似乎无法得到正确的响应。如果有人能告诉我我做错了什么,我将不胜感激。代码:
name = input("Type a string!")
name = name.upper
lenname = len(name)
if lenname == 3:
print("Success")
else:
print("Fail")
我希望输出是Success
当我输入abc
但我收到:
TypeError Traceback (most recent
call last)
<ipython-input-20-1de45569cf05> in <module>
1 name = input("Type a string!")
2 name = name.upper
----> 3 lenname = len(name)
4 if lenname == 3:
5 print("Sucess")
TypeError: object of type 'builtin_function_or_method' has no len()
解决方案
您正在尝试upper
像访问字段而不是方法一样访问。在上面你使用的那一行len
,做name = name.upper()
。
推荐阅读
- javascript - JS - 使用 FileReader 对象时只允许上传一个文件
- html - 如何在移动布局中居中文本?
- javascript - 电子邮件表格适用于我的网站的一个版本,但不适用于另一个版本?
- java - 代理类中的空依赖项
- ios - 如何在更新它显示的对象数组时更新 SwiftUI 中的 ScrollView?
- python - Dask Dataframe .read_csv 不尊重 dtypes
- html - 角材料卡布局
- git - 为什么不能推送第二个克隆存储库?
- dataframe - Spark Streaming Dataframe 执行,有状态,分区本地 groupBy,避免洗牌
- arrays - 从对象数组中提取一个键值对的数组,同时基于另一个键值对进行过滤