【发布时间】:2019-06-20 05:59:01
【问题描述】:
我是 Python 编程的初学者 使用 PyCharm 尝试练习功能,但返回以下错误:
名称“rflag”未定义 但我认为它的定义! 这是代码:
def searcher(word: str, text: str, num: int = 1):
global startindex
global size
global rflag
if num == 1 and text.count(word) == 1:
startindex = text.find(word);
size = len(word);
rflag = "word start from " + str(startindex + 1) + " and end in " +
str(size + startindex)
elif num > 1 and text.count(word) <= num:
startindex = 0
for i in range(num):
startindex = text.find(word, startindex)
size = startindex + len(word)
rflag = "word start from " + str(startindex + 1) + " and end in " +
str(size + startindex)
return rflag
result = searcher("shahab", "shahabshahabshahab", 2)
print(result)
完整的错误信息:
C:\Users\Shahab\AppData\Local\Programs\Python\Python37-32\python.exe C:/Users/Shahab/Desktop/searcher.py
Traceback(最近一次调用最后一次):
文件“C:/Users/Shahab/Desktop/searcher.py”,第 21 行,在 result = searcher("shahab", "shahabshahabshahab", 2) 文件“C:/Users/Shahab/Desktop/searcher.py”,第 18 行,在搜索器中 return rflag NameError: name 'rflag' is not defined
进程以退出代码 1 结束
【问题讨论】:
-
这段代码应该做什么?
-
请修正你的缩进...
-
@Aran-Fey 没有什么只是练习的样本
-
@hiroprotagonist 图像添加缩进
-
如果我们不知道它应该做什么,我们无法修复您的代码...
标签: python function global-variables