def thanks(don_dict): """Return updated donation list with new name and amount""" # Prompt for full name, list of names, or quit. Add a name to don_dict. while True: name = u'' + safe_input("Type full name for Thank You letter \ or [list] for existing donors. (or [quit]): ") if name.lower() == u'list': for donor in don_dict.keys(): print donor elif name == u'quit': # Exit thanks() and return original don_dict return don_dict else: don_dict.setdefault(name, []) break # Promt for amount until float amount = u'' + safe_input("What is %s's donation amount? \ (or [quit]) " % name) if amount == u'quit': # Exit thanks() and return don_dict w/ possible new name & no donations return don_dict while True: try: amount = float(amount) except ValueError: amount = u'' + safe_input("%s is not a number, please enter \ donation amount [or 'quit']: " % amount) if amount == u'quit': return don_dict else: break # Add amount to current donor's list don_dict[name].append(amount) # Create file try: os.mkdir('./mail') print './mail folder created for saved letters.' except OSError: print './mail folder exists for saved letters.' l_file = "./mail/{}_ty.txt".format(name) letter = codecs.open(l_file, 'w') letter.write("Dear {}, \n\n\ \tThank you for your donation of ${:.2f} to our charity. We appreciate \ your support.\n\n\ Sincerely,\nMichelle Rascati".format(name, amount)) letter.close() print 'Letter saved to ' + l_file return don_dict
def show_main_menu(): ''' Present the menu to find out what the user wants to do. ''' ''' Returns an int giving the menu item selected. ''' while True: print("") print("MAIN MENU") print("(1) Send a Thank You") print("(2) Print Report") print("(3) Exit") choice = safe_input("Choice --> ") try: choice = int(choice) if choice not in [1, 2, 3]: print("Invalid selection. Please try again.") else: break except: print("Invalid selection. Please try again.") finally: pass return choice
def thank(donor_list): ''' Carries out the tasks necessary to enter, possibly create a new donor, add a donation amount to the donor, and create a thank-you letter. :param donor_list: List object containing donor first name, last name, and list of donation amounts :return: Nothing ''' while True: print("") cmd = input("[Full Name|'list'|'thank_all'|'back'] --> ").lower() if cmd == "list": print_donors(donors) continue elif cmd == "back": break elif cmd == "thank_all": # The user wants to print thank-you's to all donors # Loop over all the donors, find the total sum donated. Then craft # a thank-you message and write it to both the terminal and to disk. for donor in donor_list: donor_name = donor[0] donation_sum = sum(donor[1]) msg = ("\n" "Dear {0}, \n" "\n" "Thank you for your generous donations totalling ${1}.\n" "Trundle and Biffs all over the world will benefit from your kindness!\n" "\n" "Yours Truely,\n" " Bing Flaherty").format(donor_name, donation_sum) print(msg) with open("{0}_thank_you.txt".format(donor_name), "w") as fd: fd.writelines(msg) else: # The user provided a name to add to the list. # Adding a donation and printing a thank-you. # First check if the donor is in the list already, if not add them. full_name = cmd.title() if not(is_donor_in_list(donors, full_name)): donor_list.append([full_name, []]) # Get the donation amount... while True: amnt = safe_input("Donation amount --> ") try: amnt = int(amnt) break except: print("Invalid donation amount. Please try again. Input only numbers") finally: pass # Find the donor entry in the donor list and append the donation amount donor_entry_idx = find_donor_idx(donor_list, full_name) donor_entry = donor_list[donor_entry_idx] donor_entry[1].append(amnt) # Compose thank-you message and print to the terminal and a file msg = ("\n" "Dear {0}, \n" "\n" "Thank you for your generous donation of ${1}.\n" "Trundle and Biffs all over the world will benefit from your kindness!\n" "\n" "Yours Truely,\n" " Bing Flaherty").format(full_name, amnt) print(msg) with open("{0}_thank_you.txt".format(full_name), "w") as fd: fd.writelines(msg)
def thank(donor_list): ''' Carries out the tasks necessary to enter, possibly create a new donor, add a donation amount to the donor, and create a thank-you letter. :param donor_list: List object containing donor first name, last name, and list of donation amounts :return: Nothing ''' while True: print("") cmd = input("[Full Name|'list'|'thank_all'|'back'] --> ").lower() if cmd == "list": print_donors(donors) continue elif cmd == "back": break elif cmd == "thank_all": # The user wants to print thank-you's to all donors # Loop over all the donors, find the total sum donated. Then craft # a thank-you message and write it to both the terminal and to disk. for donor in donor_list: donor_name = donor[0] donation_sum = sum(donor[1]) msg = ( "\n" "Dear {0}, \n" "\n" "Thank you for your generous donations totalling ${1}.\n" "Trundle and Biffs all over the world will benefit from your kindness!\n" "\n" "Yours Truely,\n" " Bing Flaherty").format(donor_name, donation_sum) print(msg) with open("{0}_thank_you.txt".format(donor_name), "w") as fd: fd.writelines(msg) else: # The user provided a name to add to the list. # Adding a donation and printing a thank-you. # First check if the donor is in the list already, if not add them. full_name = cmd.title() if not (is_donor_in_list(donors, full_name)): donor_list.append([full_name, []]) # Get the donation amount... while True: amnt = safe_input("Donation amount --> ") try: amnt = int(amnt) break except: print( "Invalid donation amount. Please try again. Input only numbers" ) finally: pass # Find the donor entry in the donor list and append the donation amount donor_entry_idx = find_donor_idx(donor_list, full_name) donor_entry = donor_list[donor_entry_idx] donor_entry[1].append(amnt) # Compose thank-you message and print to the terminal and a file msg = ( "\n" "Dear {0}, \n" "\n" "Thank you for your generous donation of ${1}.\n" "Trundle and Biffs all over the world will benefit from your kindness!\n" "\n" "Yours Truely,\n" " Bing Flaherty").format(full_name, amnt) print(msg) with open("{0}_thank_you.txt".format(full_name), "w") as fd: fd.writelines(msg)
count="Count", average="Average") for donor in don_rep: print "{name:<{n}} {total:>8.2f} {count:>8} {average:>8.2f}".\ format(n=n_long, name=donor[0], total=donor[1], count=donor[2], average=donor[3]) def second(l_list): """Return second variable in list.""" return l_list[1] if __name__ == '__main__': donations = {u'Larry': [10.00, 150.50, 75.00], u'Sue': [40.00, 35.00], u'Julie': [35.50], u'Bob': [60.25, 100.00], u'Karen': [83.50, 72.45, 90.25]} while True: do = u'' while do.lower() not in (u'ty', u'cr', u'quit'): do = u'' + safe_input("Send a Thank You [ty] or Create a Report \ [cr] or [quit]? ") if do.lower() == u'ty': donations = thanks(donations) elif do.lower() == u'cr': create(donations) elif do.lower() == u'quit': break