【发布时间】:2014-03-23 00:11:40
【问题描述】:
我正在尝试编写一个函数,该函数使用 for 循环和 isupper 方法仅打印字符串的大写字母。
到目前为止我做了什么:
upper_chars = ""
def only_upper(s):
for char in s:
if s.isupper() == True:
upper_chars += char
print upper_chars
但这不起作用?有人告诉我为什么吗? 我收到此错误消息:“UnboundLocalError: local variable 'upper_chars' referenced before assignment”
【问题讨论】:
标签: python string python-2.7