I am looking for some help with loops. I have a name list, and I'm looking to sequentially message each profile from that list with a random user inputted message.
Ideally this would involve looping through the profileLink items while simultaneously looping through the nameList items so that I can build a new messageInput for each profile.
My code works, but currently has a lot of duplication and to build this to support a larger list will involve a lot of copying and pasting, as you can see below - How would you go about this?
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')
EDIT: After trying the top answer, I got the error that list object has no attribute 'replace'
Worth mentioning my list is built using str.replace/str.split functions, like so:
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]
EDIT 2: First edit was actually an unrelated error. Fixed now.