Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
功能创建—“功能创建”;未定义名称";-python_Python_String_Function_Python 2.7_Undefined - Fatal编程技术网

功能创建—“功能创建”;未定义名称";-python

功能创建—“功能创建”;未定义名称";-python,python,string,function,python-2.7,undefined,Python,String,Function,Python 2.7,Undefined,我正在编写一些代码,从文本文件中读取单词并将它们分类到字典中。它实际上运行良好,但供参考: 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 start

我正在编写一些代码,从文本文件中读取单词并将它们分类到字典中。它实际上运行良好,但供参考:

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, )
错误消息是未定义的名称“hello”

我不知道如何修改函数参数,以便在没有语音标记的情况下输入它们


谢谢

简单,您可以定义该名称:

class hello:
  txt = "hello.txt"

但开玩笑的是,函数调用中的所有参数值都是表达式。如果要按字面形式传递字符串,则必须使用引号将字符串转换为文字。Python不像m4或cpp那样是一个文本预处理器,它希望整个程序文本都遵循其语法

所以我只是误解了被问到的问题。我已经让课程负责人澄清了

正如我现在完全知道的,在输入字符串时需要告知函数定义,因此需要引号

我承认我完全不知道我对它的理解有多深——我认为你可以把任何字母和/或数字组合作为参数,然后你可以在函数定义中操纵它们

<>我的无知可能源于我对Python很陌生,在C++上学习了我的编码基础,如果我记得正确的话(一年多前),函数被定义为每个参数都被具体地设置为它们的类型,例如

int max(int num1, int num2)
而在Python中,您并不是这样做的

谢谢你的帮助(和嘲笑!)


现在问题已经解决。

这些“语音标记”称为引号,是解释器区分字符串和标识符的唯一方法。是的,机器是哑的,而解释器也是哑的,这就是生活。你为什么要输入不带引号的参数?如果这个错误消息或其原因真的让你困惑,那么我怀疑你是否真的写了这段代码,或者理解了它。提示:例如,在“如果单词[0]==finish:”的行中,您是否想过尝试使用
如果单词[0]==finish:
?你能理解为什么那不起作用吗?在这个等式的另一面,你能看到为什么
单词[0]
不总是计算为
“w”
?@KarlKnechtel,显然他没有写代码。斯图尔特,老实说,我不知道我当时为什么写这个。听起来很傲慢。