我正在尋找一些有關回圈的幫助。我有一個姓名串列,我希望使用隨機用戶輸入的訊息從該串列中按順序向每個組態檔發送訊息。
理想情況下,這將涉及遍歷 profileLink 項,同時遍歷 nameList 項,以便我可以為每個組態檔構建一個新的 messageInput。
我的代碼有效,但目前有很多重復項,要構建它以支持更大的串列將涉及大量復制和粘貼,如下所示 - 你會如何處理?
messageInput0="Hi " f'{namelist[0]}' ", " f'{choice(messageOptions)}'
messageInput1="Hi " f'{namelist[1]}' ", " f'{choice(messageOptions)}'
messageInput2="Hi " f'{namelist[2]}' ", " f'{choice(messageOptions)}'
messageInput3="Hi " f'{namelist[3]}' ", " f'{choice(messageOptions)}'
messageInput4="Hi " f'{namelist[4]}' ", " f'{choice(messageOptions)}'
webbrowser.open(profileLink[0])
time.sleep(6)
pyautogui.press('tab')
pyautogui.write(messageInput0, interval=random.uniform(0.03, 0.15))
with pyautogui.hold('command'):
pyautogui.press('w')
webbrowser.open(profileLink[1])
time.sleep(6)
pyautogui.press('tab')
pyautogui.write(messageInput1, interval=random.uniform(0.03, 0.15))
with pyautogui.hold('command'):
pyautogui.press('w')
webbrowser.open(profileLink[2])
time.sleep(6)
pyautogui.press('tab')
pyautogui.write(messageInput2, interval=random.uniform(0.03, 0.15))
with pyautogui.hold('command'):
pyautogui.press('w')
webbrowser.open(profileLink[3])
time.sleep(6)
pyautogui.press('tab')
pyautogui.write(messageInput3, interval=random.uniform(0.03, 0.15))
with pyautogui.hold('command'):
pyautogui.press('w')
webbrowser.open(profileLink[4])
time.sleep(6)
pyautogui.press('tab')
pyautogui.write(messageInput4, interval=random.uniform(0.03, 0.15))
with pyautogui.hold('command'):
pyautogui.press('w')
編輯:在嘗試了最佳答案后,我得到了串列物件沒有屬性“替換”的錯誤
值得一提的是,我的串列是使用 str.replace/str.split 函式構建的,如下所示:
profileLink = open("profiles.txt").read().splitlines()
profileLink = [item.replace(" ","name=") for item in profileLink]
newLinks = [item.replace("%","name=") for item in profileLink]
nameList = [i.split("name=")[1] for i in newLinks]
編輯 2:第一次編輯實際上是一個不相關的錯誤。現在修好了。
uj5u.com熱心網友回復:
像這樣。
- 用于
zip“壓縮”名稱和組態檔鏈接,您不必跟蹤回圈索引。
import random
import time
import pyautogui
import webbrowser
namelist = ["a", "b", "c", "d"]
profilelink = ["q", "w", "e", "r"]
messageOptions = ["bye", "hello"]
for name, link in zip(namelist, profilelink):
message = f"Hi {name}, {random.choice(messageOptions)}"
webbrowser.open(link)
time.sleep(6)
pyautogui.press("tab")
pyautogui.write(message, interval=random.uniform(0.03, 0.15))
with pyautogui.hold("command"):
pyautogui.press("w")
uj5u.com熱心網友回復:
這就是我的做法:
import random
import time
import webbrowser
def messageInput(nameList, profileLink):
for name in nameList:
message = "Hi " f'{name}' ", " f'{choice(messageOptions)}'
for profile in profileLink:
webbrowser.open(profile)
time.sleep(6)
pyautogui.press('tab')
pyautogui.write(message, interval=random.uniform(0.03, 0.15))
with pyautogui.hold('command'):
pyautogui.press('w')
uj5u.com熱心網友回復:
使用for回圈:
for i in range(len(profileLink)):
webbrowser.open(profileLink[i])
time.sleep(6)
pyautogui.press('tab')
message = f'Hi {namelist[i]}, {choice(messageOptions)}'
pyautogui.write(message, interval=random.uniform(0.03, 0.15))
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/510410.html
標籤:Python循环
上一篇:Python嵌套for回圈,排序
下一篇:如何迭代R中的函式?
