[split] capitalize dict keys for display in string - 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: [split] capitalize dict keys for display in string (/thread-21698.html) |
[split] capitalize dict keys for display in string - newbieAuggie2019 - Oct-10-2019 (Oct-10-2019, 07:09 AM)perfringo Wrote: In order to save repeated typing one can use dictionaries and try...except: Hi! I'm not sure if it is appropriate for me to ask a question here, as it is not my thread. If it is not appropriate, my apologies, and I'd be grateful, relieved and agreeable if it is moved to a new thread. Well, my question is that you, as usual, made wise suggestions to my always improvable code. (I'm very grateful to you for that, as it makes me widen my knowledge.) I made the necessary adjustments to my code and got this: # license_02.py # email1 = input('Please, enter your email:\n') subject1 = input('Please, enter your Message Subject:\n') subject1 = subject1.lower() messageBody1 = input('Please, enter your Message Body:\n') header = (f'Dear customer {email1},\n\n' 'Thank you for contacting our company.') footer = 'Thank you\n\nCompany customer service' categories = {'i lost my license': 'You will be contacted via email to verify your license request.\n\n', 'problem saving file': 'Saving file is done by pressing CTRL-S (Windows} or CMD- (Mac).\n\n'} def getList(categories): for cat in categories.keys(): return cat.capitalize() try: print(f'{header}{categories[subject1.lower()]}{footer}') except KeyError: print(f"Sorry, you should write, word for word, one of these: '{getList(categories)}' on the message subject.")and it works great on the two categories shown, but when I tried to improve also my code at the except KeyError: , by wanting to display all the keys of the dictionary, like this ideal output: I managed to capitalize back the keys of the dictionary, but the only key printed is the first one: As usual, I tried different approaches, getting all kinds of error:
Once again, I apologise if I'm posting in the wrong place. Please be free to move it where you see it feasible.Thanks and all the best, RE: [split] Python Automated Response System Help - buran - Oct-10-2019 as it is now your getList will return just the first key (see the indentation of the return statement)def getList(categories): for cat in categories.keys(): return cat.capitalize()probably you want def getList(categories): return [cat.capitalize() for cat in categories.keys()] # this will return list of keys() # return ', '.join(cat.capitalize() for cat in categories.keys()) # this will return comma separated stringif you get list from function you will need to construct the comma separated string in the f-string print(f"Sorry, you should write, word for word, one of these: \"{', '.join(getList(categories))}\" on the message subject.")or you can directly change the print function print(f"Sorry, you should write, word for word, one of these: \"{', '.join(map(str.capitalize, categories.keys()))}\" on the message subject.") RE: [split] Python Automated Response System Help - underworld_sigil - Oct-10-2019 Hello, Your getList function returns immediately after the first iteration because the return statement is inside the for loop. If you want it to return a result with all the string capitalized in it, build a list and return it after the loop, for example : mylist = [] for cat in categories.keys(): mylist.append(cat.capitalize()) return mylist RE: [split] Python Automated Response System Help - perfringo - Oct-10-2019 Something like below? >>> f"You must pick one from: {', '.join(key.capitalize() for key in categories)}" 'You must pick one from: I lost my license, Problem saving file'Or with quotation marks: >>> f"""You must pick one from: {', '.join(f'"{key.capitalize()}"' for key in categories)}""" 'You must pick one from: "I lost my license", "Problem saving file"' |