2012-07-01 108 views
1

這是我的一段代碼:NameError:名字「書」沒有定義

#!/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 

爲什麼呢?

回答

2

input()的作用類似於eval(raw_input())爲Python 2:

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

    Equivalent to eval(raw_input(prompt)). 

你可能尋找raw_input()

相關問題