[英]How can I generate a text using generator in python?
我想使用 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()
提前致謝
這是您可以執行的操作的基本示例。 您需要迭代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
您的代碼更適合初學者的版本是:
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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.