python - 名称错误 : name 'book' is not defined

标签 python string

这是我的代码:

#!/usr/bin/python 

from xml.etree.ElementTree import ElementTree
from array import *
#import re

count = array('i',[0,0,0])

def find_root_tags(file,str1,i):

    tree = ElementTree()
    tree.parse(file)
    root = tree.getroot()
    if root == str1:
        count[i] = count[i]+1

    for j in tree.getiterator(str1):
        count[i] = count[i] + 1

    print "%dth file's count %d" % (i,count[i])


str1 = input("enter the word to be searched:")
fo = open("xml.txt","r")

for i in range(count.__len__()):

    file = fo.readline()
    find_root_tags(file,str1,i)

fo.close()

运行代码时出现此错误:

enter the word to be searched:book

Traceback (most recent call last):

File "keywords.py", line 23, in <module>

str1 = input("enter the word to be searched\n")

File "<string>", line 1, in <module>

NameError: name 'book' is not defined

为什么?

最佳答案

input() 的作用类似于 Python 2 的 eval(raw_input()):

input(...)
    input([prompt]) -> value

    Equivalent to eval(raw_input(prompt)).

您可能正在寻找raw_input()

关于python - 名称错误 : name 'book' is not defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11280194/

相关文章:

c - 字符串的快速排序数组在 C 中给出错误

swift - 从 Swift 中的一系列字符初始化字符串

php - 使用 = 和 & 作为分隔符解析字符串,但并非所有 & 都是具有前瞻性的分隔符

python - 如何循环打印模型的所有 tf​​.Tensors?

python - 如何在没有 DFS/BFS 的情况下枚举哈密顿循环的可能重建?

python - Django CORS X-FirePHP 版本

python - 数值积分过程中两个物体之间的距离保持恒定

python - 使用python检查opencv中的轮廓区域

string - 收缩字符串编码算法

Javascript:如何强制具有十进制值的字符串使用点而不是逗号?