Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import smtplib
- from email.message import EmailMessage
- from getpass import getpass
- name = str(input("Name: "))
- email = str(input("Email: "))
- password = str(getpass("Password: "))
- recipient = str(input("Recipient's email: "))
- while True:
- try:
- productsnm = int(input(f"Hi, {name}!nHow many products do you want to add to the shopping list? " ))
- except ValueError:
- print ("Please input a number.")
- else:
- break
- products = []
- quantities = []
- for x in range(int(productsnm)):
- product = str(input("Input the product name: "))
- while True:
- try:
- quantity = int(input("Input the product quantity: "))
- except ValueError:
- print ("Please input a number.")
- else:
- break
- products.append(product)
- quantities.append(quantity)
- print ("nThese products have been added to the shopping list:")
- for x, y in zip(products, quantities):
- print (f'{x} x {y}')
- gmail_user = email
- gmail_password = password
- msg = EmailMessage()
- msg['Subject'] = "Shopping List"
- msg['From'] = gmail_user
- msg['To'] = [recipient]
- message = ""
- for i in range(max(len(products), len(quantities))):
- message = message + str(products[i]) + " x " + str(quantities[i]) + "n"
- msg.set_content(message)
- try:
- s = smtplib.SMTP_SSL('smtp.gmail.com', 465)
- s.ehlo()
- s.login(gmail_user, gmail_password)
- s.send_message(msg)
- s.quit()
- print ("nThe email has been sent.")
- except:
- print ("nAn error occurred.")
- print ("nHave a nice day!")
- exit()
- def get_user_input(message, type=str):
- """Ask the user for input using `message`.
- Enforce that the input is castable to `type` and cast it.
- """
- while True:
- try:
- return type(input(message))
- except ValueError:
- print (f"Please input a {type}.")
- def add_item(shopping_list):
- """Add an item to the shopping list.
- If the item already exists in the list, add the quantity to it.
- """
- name = get_user_input("Input the product name: ")
- quantity = get_user_input("Input the product quantity: ", int)
- shopping_list[name] += quantity
- def print_list(shopping_list):
- for name, quantity in shopping_list.items():
- print(name, "x", quantity)
- def email_to(shopping_list, from_email, password, *recipients):
- email = EmailMessage()
- email['Subject'] = "Shopping List"
- email['From'] = from_email
- email['To'] = recipients
- message = "n".join(f"{name} x {quantity}" for name, quantity in shopping_list.items())
- email.set_content(message)
- try:
- s = smtplib.SMTP_SSL('smtp.gmail.com', 465)
- s.ehlo()
- s.login(from_user, password)
- s.send_message(email)
- s.quit()
- print ("nThe email has been sent.")
- except Exception as e:
- print ("nAn error occurred:", e)
- import smtplib
- from email.message import EmailMessage
- from getpass import getpass
- from collections import defaultdict
- if __name__ == "__main__":
- name = input("Name: ")
- n = get_user_input(f"Hi, {name}!nHow many products do you want to add to the shopping list? ", int)
- shopping_list = defaultdict(int)
- for _ in range(n):
- add_item(shopping_list)
- print_list(shopping_list)
- email = input("Email: ")
- password = getpass("Password: ")
- recipient = input("Recipient's email: ")
- email_to(shopping_list, email, password, recipient)
- class ShoppingList:
- def __init__(self):
- self.items = defaultdict(int)
- def __str__(self):
- return "n".join(f"{name} x {quantity}" for name, quantity in self.items.items())
- def add_item(self, name, quantity):
- self.items[name] += quantity
- def email_to(self, from_email, password, *recipients):
- ...
- if __name__ == "__main__":
- name = input("Name: ")
- n = get_user_input(f"Hi, {name}!nHow many products do you want to add to the shopping list? ", int)
- shopping_list = ShoppingList()
- for _ in range(n):
- name = get_user_input("Input the product name: ")
- quantity = get_user_input("Input the product quantity: ", int)
- shopping_list.add_item(name, quantity)
- print(shopping_list)
- email = input("Email: ")
- password = getpass("Password: ")
- recipient = input("Recipient's email: ")
- shopping_list.email_to(email, password, recipient)
- from collections import namedtuple
- Product = namedtuple('Product', ['name', 'quantity'])
- # inside the for-loop:
- products.append(Product(name=product_name, quantity=quantity))
- [Product(name='n0', quantity=0), Product(name='n1', quantity=1), Product(name='n2', quantity=32)]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement