函数创建 - "Undefined name" - Python
Function creation - "Undefined name" - Python
我正在编写一些代码,从文本文件中读取单词并将它们分类到字典中。它实际上运行良好,但这里供参考:
def find_words(file_name, delimiter = " "):
"""
A function for finding the number of individual words, and the most popular words, in a given file.
The process will stop at any line in the file that starts with the word 'finish'.
If there is no finish point, the process will go to the end of the file.
Inputs: file_name: Name of file you want to read from, e.g. "mywords.txt"
delimiter: The way the words in the file are separated e.g. " " or ", "
: Delimiter will default to " " if left blank.
Output: Dictionary with all the words contained in the given file, and how many times each word appears.
"""
words = []
dictt = {}
with open(file_name, 'r') as wordfile:
for line in wordfile:
words = line.split(delimiter)
if words[0]=="finish":
break
# This next part is for filling the dictionary
# and correctly counting the amount of times each word appears.
for i in range(len(words)):
a = words[i]
if a=="\n" or a=="":
continue
elif dictt.has_key(a)==False:
dictt[words[i]] = 1
else:
dictt[words[i]] = int(dictt.get(a)) + 1
return dictt
问题是它只有在参数以字符串文字形式给出时才有效,例如,这个有效:
test = find_words("hello.txt", " " )
但这不是:
test = find_words(hello.txt, )
错误信息是undefined name 'hello'
我不知道如何更改函数参数,以便我可以在没有语音标记的情况下输入它们。
谢谢!
简单,你定义那个名字:
class hello:
txt = "hello.txt"
但开个玩笑,函数调用中的所有参数值都是表达式。如果您想按字面意思传递字符串,则必须使用引号创建字符串文字。 Python 不是像 m4 或 cpp 这样的文本预处理器,它希望整个程序文本都遵循其语法。
所以事实证明我只是误解了所问的内容。我已经让课程负责人澄清了。
据我所知,当输入字符串时需要告知函数定义,因此需要引号。
我承认我完全不知道我对这一切如何运作的理解深度 - 我认为你几乎可以将任何字母 and/or 数字作为参数放入,然后你可以操纵它们 在函数定义中。
我的无知可能源于我对 Python 很陌生,我已经学习了 C++ 的编码基础知识,如果我没记错的话(一年多以前),函数是定义每个参数都被专门设置为它们的类型,例如
int max(int num1, int num2)
而在 Python 中,您并不是那样做的。
感谢您的帮助(和嘲笑!)
问题现已解决。
我正在编写一些代码,从文本文件中读取单词并将它们分类到字典中。它实际上运行良好,但这里供参考:
def find_words(file_name, delimiter = " "):
"""
A function for finding the number of individual words, and the most popular words, in a given file.
The process will stop at any line in the file that starts with the word 'finish'.
If there is no finish point, the process will go to the end of the file.
Inputs: file_name: Name of file you want to read from, e.g. "mywords.txt"
delimiter: The way the words in the file are separated e.g. " " or ", "
: Delimiter will default to " " if left blank.
Output: Dictionary with all the words contained in the given file, and how many times each word appears.
"""
words = []
dictt = {}
with open(file_name, 'r') as wordfile:
for line in wordfile:
words = line.split(delimiter)
if words[0]=="finish":
break
# This next part is for filling the dictionary
# and correctly counting the amount of times each word appears.
for i in range(len(words)):
a = words[i]
if a=="\n" or a=="":
continue
elif dictt.has_key(a)==False:
dictt[words[i]] = 1
else:
dictt[words[i]] = int(dictt.get(a)) + 1
return dictt
问题是它只有在参数以字符串文字形式给出时才有效,例如,这个有效:
test = find_words("hello.txt", " " )
但这不是:
test = find_words(hello.txt, )
错误信息是undefined name 'hello'
我不知道如何更改函数参数,以便我可以在没有语音标记的情况下输入它们。
谢谢!
简单,你定义那个名字:
class hello:
txt = "hello.txt"
但开个玩笑,函数调用中的所有参数值都是表达式。如果您想按字面意思传递字符串,则必须使用引号创建字符串文字。 Python 不是像 m4 或 cpp 这样的文本预处理器,它希望整个程序文本都遵循其语法。
所以事实证明我只是误解了所问的内容。我已经让课程负责人澄清了。
据我所知,当输入字符串时需要告知函数定义,因此需要引号。
我承认我完全不知道我对这一切如何运作的理解深度 - 我认为你几乎可以将任何字母 and/or 数字作为参数放入,然后你可以操纵它们 在函数定义中。
我的无知可能源于我对 Python 很陌生,我已经学习了 C++ 的编码基础知识,如果我没记错的话(一年多以前),函数是定义每个参数都被专门设置为它们的类型,例如
int max(int num1, int num2)
而在 Python 中,您并不是那样做的。
感谢您的帮助(和嘲笑!)
问题现已解决。