在“For”循环内调用一个函数会引发一个NameError [英] Calling a function inside 'For' loop throws a NameError

查看:259
本文介绍了在“For”循环内调用一个函数会引发一个NameError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从FOR循环中调用一个函数,但出现错误:

  test()
NameError:name'test'未定义

代码如下:

$来自selenium import webdriver的

$ b从selenium.common.exceptions导入NoSuchElementException
从selenium.webdriver.common.keys导入键
导入时间
从lxml导入html
导入请求
导入xlwt

browser = webdriver.Firefox()#获取本地会话的firefox

#0等待页面加载
browser.implicitly_wait(3)#3秒应该够了。如果没有,增加

browser.get(http://ae.bizdirlib.com/taxonomy/term/1493)#加载页
links = browser.find_elements_by_css_selector(h2 $ a


链接中的链接:
link.send_keys(Keys.CONTROL + Keys.RETURN)
link.send_keys(Keys.CONTROL + Keys.PAGE_UP)
time.sleep(5)
test()
link.send_keys(Keys.CONTROL +'w')



def test(self):#test function
elems = browser.find_elements_by_css_selector(div.content.clearfix> div> fieldset> div> ul> li> span)

elem in elems:
print elem.text
elem1 = browser.find_elements_by_css_selector(div.content.clearfix> div> fieldset> div> ul> li> a)

elem1中的elems21
print elems21.text
返回0

所以我想调用该函数,当函数调用时,我希望数据被复制/粘贴到Excel中。



有人可以帮助我增强代码吗?

解决方案

你必须创建一个函数对象才能使用它。在你的情况下,你调用一个函数,但它还不存在,因此没有定义。正如Kevin所说,定义函数,然后尝试调用它。



UPD:我不能添加评论,所以我在这里更新。 Mark Lutz在学习Python书中详细描述了它的功能,功能如何工作,什么是 def ,当您调用函数时会发生什么。但是我认为任何其他的Python书都会这样做。



UPD:写评论并不容易,所以我更新了答案。



如上所述,问题是你调用它后定义函数。例如,假设我想编写一个写有乐趣的任何名称的程序。为简单起见,程序中给出了名称。
选项1:如果我写的程序就像你的意思1)调用函数2)定义一个函数我将得到一个名称错误,就像你得到的。



程序:

  greet ='玩得开心'

print(greet + name '))#我调用一个函数'name'

def name(x):#我定义一个函数
return str(x)

输出将为:

 追溯(最多最近的电话最后):
文件C:/Users/nikolay.dudaev/Documents/Private/deffun2.py,第3行,< module>
print(greet + name('John'))
NameError:name'name'未定义

我需要做的是更改功能定义和调用函数的位置:

  greet = '

def name(x):#定义一个函数
return str(x)

print(greet + name('John' ))#我调用一个函数'name'

现在的输出是:

  ======= RESTART:C:/Users/nikolay.dudaev/Documents/Private/deffun2.py ====== = 
玩得开心,John
>>>

你去!



复制什么你在 def 之后,将它粘贴在之前循环,它应该可以工作(虽然我没有尝试你的代码) 。


I'm trying to call a function from a FOR loop but get the error:

 test()
NameError: name 'test' is not defined

The code is given below:

from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys
import time
from lxml import html
import requests
import xlwt

browser = webdriver.Firefox() # Get local session of firefox

# 0 wait until the pages are loaded
browser.implicitly_wait(3) # 3 secs should be enough. if not, increase it

browser.get("http://ae.bizdirlib.com/taxonomy/term/1493") # Load page
links = browser.find_elements_by_css_selector("h2 > a")


for link in links:
    link.send_keys(Keys.CONTROL + Keys.RETURN)
    link.send_keys(Keys.CONTROL + Keys.PAGE_UP)
    time.sleep(5)
    test()
    link.send_keys(Keys.CONTROL + 'w')



def test(self):#test function
    elems = browser.find_elements_by_css_selector("div.content.clearfix > div > fieldset> div > ul > li > span")

    for elem in elems:
        print elem.text
    elem1 = browser.find_elements_by_css_selector("div.content.clearfix>div>fieldset>div>ul>li>a")

    for elems21 in elem1:
        print elems21.text 
    return 0

So I want to call the function and when the function is called I want the data to be copy/pasted into Excel.

Can somebody help me to enhance the code ?

解决方案

You have to create a function object before you can use it. In your case you call a function but it is not yet existing hence not defined. As Kevin said, define the function and then try to call it.

UPD: I cannot add comments so I update it here. Mark Lutz in his "Learning Python" book describes it in a great detail, how functions work, what def does and what happens when you call a function. But I assume any other Python book will do the same.

UPD: It is not easy to write comments, so I update the answer.

As said the problem is that you define function after you call it. For example, let's assume I want to write a program which writes "Have fun, " + any name. For simplicity name is given in the program. Option 1: If I write the program like you did meaning 1) call a function 2) define a function I will get an NameError exactly like you get.

Program:

greet = 'Have fun, '

print(greet + name('John')) # I call a function 'name'

def name(x):                # I define a function
    return str(x)

The output will be:

Traceback (most recent call last):
  File "C:/Users/nikolay.dudaev/Documents/Private/deffun2.py", line 3, in <module>
    print(greet + name('John'))
NameError: name 'name' is not defined

ALL that I need to do is change places of function definition and calling a function:

greet = 'Have fun, '

def name(x):                # I define a function
    return str(x)

print(greet + name('John')) # I call a function 'name'

And now the output is:

======= RESTART: C:/Users/nikolay.dudaev/Documents/Private/deffun2.py =======
Have fun, John
>>> 

Here you go!

Copy what you have after def and paste it before your for loop and it should work (though I did not try your code).

这篇关于在“For”循环内调用一个函数会引发一个NameError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆