Advertisement
Guest User

Untitled

a guest
Feb 26th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.59 KB | None | 0 0
  1. import smtplib
  2.  
  3. from email.message import EmailMessage
  4.  
  5. from getpass import getpass
  6.  
  7. name = str(input("Name: "))
  8. email = str(input("Email: "))
  9. password = str(getpass("Password: "))
  10. recipient = str(input("Recipient's email: "))
  11.  
  12. while True:
  13. try:
  14. productsnm = int(input(f"Hi, {name}!nHow many products do you want to add to the shopping list? " ))
  15. except ValueError:
  16. print ("Please input a number.")
  17. else:
  18. break
  19.  
  20. products = []
  21. quantities = []
  22.  
  23. for x in range(int(productsnm)):
  24.  
  25. product = str(input("Input the product name: "))
  26.  
  27. while True:
  28. try:
  29. quantity = int(input("Input the product quantity: "))
  30. except ValueError:
  31. print ("Please input a number.")
  32. else:
  33. break
  34.  
  35. products.append(product)
  36. quantities.append(quantity)
  37.  
  38. print ("nThese products have been added to the shopping list:")
  39.  
  40. for x, y in zip(products, quantities):
  41. print (f'{x} x {y}')
  42.  
  43. gmail_user = email
  44. gmail_password = password
  45.  
  46. msg = EmailMessage()
  47. msg['Subject'] = "Shopping List"
  48. msg['From'] = gmail_user
  49. msg['To'] = [recipient]
  50. message = ""
  51. for i in range(max(len(products), len(quantities))):
  52. message = message + str(products[i]) + " x " + str(quantities[i]) + "n"
  53. msg.set_content(message)
  54.  
  55. try:
  56. s = smtplib.SMTP_SSL('smtp.gmail.com', 465)
  57. s.ehlo()
  58. s.login(gmail_user, gmail_password)
  59. s.send_message(msg)
  60. s.quit()
  61.  
  62. print ("nThe email has been sent.")
  63.  
  64. except:
  65. print ("nAn error occurred.")
  66.  
  67. print ("nHave a nice day!")
  68.  
  69. exit()
  70.  
  71. def get_user_input(message, type=str):
  72. """Ask the user for input using `message`.
  73. Enforce that the input is castable to `type` and cast it.
  74. """
  75. while True:
  76. try:
  77. return type(input(message))
  78. except ValueError:
  79. print (f"Please input a {type}.")
  80.  
  81. def add_item(shopping_list):
  82. """Add an item to the shopping list.
  83. If the item already exists in the list, add the quantity to it.
  84. """
  85. name = get_user_input("Input the product name: ")
  86. quantity = get_user_input("Input the product quantity: ", int)
  87. shopping_list[name] += quantity
  88.  
  89. def print_list(shopping_list):
  90. for name, quantity in shopping_list.items():
  91. print(name, "x", quantity)
  92.  
  93. def email_to(shopping_list, from_email, password, *recipients):
  94. email = EmailMessage()
  95. email['Subject'] = "Shopping List"
  96. email['From'] = from_email
  97. email['To'] = recipients
  98. message = "n".join(f"{name} x {quantity}" for name, quantity in shopping_list.items())
  99. email.set_content(message)
  100.  
  101. try:
  102. s = smtplib.SMTP_SSL('smtp.gmail.com', 465)
  103. s.ehlo()
  104. s.login(from_user, password)
  105. s.send_message(email)
  106. s.quit()
  107. print ("nThe email has been sent.")
  108. except Exception as e:
  109. print ("nAn error occurred:", e)
  110.  
  111. import smtplib
  112. from email.message import EmailMessage
  113. from getpass import getpass
  114. from collections import defaultdict
  115.  
  116. if __name__ == "__main__":
  117. name = input("Name: ")
  118. n = get_user_input(f"Hi, {name}!nHow many products do you want to add to the shopping list? ", int)
  119. shopping_list = defaultdict(int)
  120. for _ in range(n):
  121. add_item(shopping_list)
  122. print_list(shopping_list)
  123.  
  124. email = input("Email: ")
  125. password = getpass("Password: ")
  126. recipient = input("Recipient's email: ")
  127. email_to(shopping_list, email, password, recipient)
  128.  
  129. class ShoppingList:
  130. def __init__(self):
  131. self.items = defaultdict(int)
  132.  
  133. def __str__(self):
  134. return "n".join(f"{name} x {quantity}" for name, quantity in self.items.items())
  135.  
  136. def add_item(self, name, quantity):
  137. self.items[name] += quantity
  138.  
  139. def email_to(self, from_email, password, *recipients):
  140. ...
  141.  
  142. if __name__ == "__main__":
  143. name = input("Name: ")
  144. n = get_user_input(f"Hi, {name}!nHow many products do you want to add to the shopping list? ", int)
  145. shopping_list = ShoppingList()
  146. for _ in range(n):
  147. name = get_user_input("Input the product name: ")
  148. quantity = get_user_input("Input the product quantity: ", int)
  149. shopping_list.add_item(name, quantity)
  150. print(shopping_list)
  151.  
  152. email = input("Email: ")
  153. password = getpass("Password: ")
  154. recipient = input("Recipient's email: ")
  155. shopping_list.email_to(email, password, recipient)
  156.  
  157. from collections import namedtuple
  158. Product = namedtuple('Product', ['name', 'quantity'])
  159. # inside the for-loop:
  160. products.append(Product(name=product_name, quantity=quantity))
  161.  
  162. [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