我想使用python生成器生成文本
我是一个初学者,最近开始学习python,我在网上搜索了但没有发现有用的东西
def make_text(n):
b = ["hello"]
yield n+b
n = ['how are you', 'what is your name']
for x in range(2):
title = driver.find_element_by_xpath('//*[@id="title"]')
title.send_keys(make_text(n))
我想得到:
hello how are you
hello what's your name?
但是我得到这个错误:
object of type 'generator' has no len()
预先感谢
答案 0 :(得分:2)
这是您可以做什么的基本示例。您需要迭代yield
ed对象
def make_text(word):
greetings = ['how are you', 'what is your name']
for greet in greetings:
yield "{} {}".format(word, greet)
def say():
texts = ['hello',]
for text in texts:
x = make_text(text)
for n in x:
print(n)
title = driver.find_element_by_xpath('//*[@id="title"]')
title.send_keys(n)
say()
输出
hello how are you
hello what is your name
答案 1 :(得分:0)
您的代码更适合初学者使用:
def make_text(n):
b = ["hello"]
return n + b
words = ['how are you', 'what is your name']
for word in words:
text = make_text(word)
print(text)