Adding an ascending number [SOLVED] - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Adding an ascending number [SOLVED] (/thread-34241.html) |
Adding an ascending number [SOLVED] - AlphaInc - Jul-10-2021 Hello everybody, I have a mail script written in python and now I want to add an ascending number in my script like this: emailContent = "Text 1" + i+1 + "Text 2" This is meant to be the information how many mails I have received. Can anyone help with this? This is my script: #!/usr/bin/env python3 import smtplib import time f1 = open("../index/mail.txt","r") mail = f1.read() [:-1] f2 = open("../index/passwd.txt","r") passwd = f2.read() [:-1] f3 = open("../index/receiver.txt","r") receiver = f3.read() [:-1] #Email Variables SMTP_SERVER = 'smtp.gmail.com' #Email Server (don't change!) SMTP_PORT = 587 #Server Port (don't change!) GMAIL_USERNAME = mail #change this to match your gmail account GMAIL_PASSWORD = passwd #change this to match your gmail password class Emailer: def sendmail(self, recipient, subject, content): #Create Headers headers = ["From: " + GMAIL_USERNAME, "Subject: " + subject, "To: " + recipient, "MIME-Version: 1.0", "Content-Type: text/html"] headers = "\r\n".join(headers) #Connect to Gmail Server session = smtplib.SMTP(SMTP_SERVER, SMTP_PORT) session.ehlo() session.starttls() session.ehlo() #Login to Gmail session.login(GMAIL_USERNAME, GMAIL_PASSWORD) #Send Email & Exit msg = (headers + "\r\n\r\n" + content).encode('utf-8') session.sendmail(GMAIL_USERNAME, recipient, msg) session.quit sender = Emailer() sendTo = receiver emailSubject = "Debug" emailContent = "Text 1" + i+1 + "Text 2" #Sends an email to the "sendTo" address with the specified "emailSubject" as the subject and "emailConten$ sender.sendmail(sendTo, emailSubject, emailContent) RE: Adding an ascending number - BashBedlam - Jul-10-2021 I assume that you are wanting to concatenate the incremented variable i with two strings. Is this what you're looking for?i = 9 emailContent = "Text 1 " + str (i+1) + " Text 2" print (emailContent) RE: Adding an ascending number - AlphaInc - Jul-11-2021 This goes into the direction. But when executing the script it stays with 10. how do I rewrite i so that i gets higher after every execution of the script ? RE: Adding an ascending number - perfringo - Jul-11-2021 IMHO sooner or later, when you least expect it, you will regret that you only open files but never close them. F-strings (formatted string literals) are around appr 5 years so it could be good time to use them. Improves readability and eliminates need to convert datatypes. If you need preserve counter between runs of program then you could write it into file and retrieve it from there. |