+ 1

First Program Tips

Hi there, First of all I just wanted to thank everyone in the community. I recently started learning Python and the Sololearn app in particular has been so helpful in allowing my to learn on the go by reading everybody's useful insights and quizzing myself constantly. I'm feeling confident enough to at least dip my toe in the water and decided to give a simple program a shot. I wrote a short bit of code that asks for user input and prints the month they were born translated to Welsh (I'm a fluent speaker): https://code.sololearn.com/c59MhPi5TE63 However, my question is this; Can the code be condensed further? Is there an easier way to write it? Furthermore, am I ticking all the boxes in terms of etiquette and style etc? Thanks for your time guys and happy learning! Rhys

7th Jan 2017, 7:41 PM
Rhys Williams
Rhys Williams - avatar
7 ответов
+ 5
#A simple program that takes user input after the prompt and translates the answer to Welsh #Using dict structure... #Removed 'result' variable ( not necessary ), and add a case if user input is not in the dict ^^ w = str(input("what month were you born in?: ")) translate = {"January":"Ionawr","February":"Chwefror","Mawrth":"Chwefror","Ebrill":"Chwefror","May":"Mai","June":"Mehefin", "July":"Gorfennaf","August":"Awst","September":"Medi","October":"Hydref","November":"Tachwedd","December":"Rhagfyr"} if (w in translate): print("The Welsh translation is "+translate[w]) else: print("I didn't know the translation of "+w)
7th Jan 2017, 8:18 PM
visph
visph - avatar
+ 3
#A simple program that takes user input after the prompt and translates the answer to Welsh # input in integer month w = int(input()) result = "The Welsh translation is " list=("Ionawr","Chwefror","Chwefror","Chwefror","Mai","Mehefin","Gorfennaf","Awst","Medi", "Hydref","Tachwedd","Rhagfyr") # why are 3 months name similar in welsh if 1<=w<=12: print("%s %s, have a good day....:)" %(result,list[w-1]))
7th Jan 2017, 8:09 PM
Sharique Ansari
Sharique Ansari - avatar
+ 2
@Rhys Williams and @Kevin: I read too quick, so I think answer of @Kevin was of @Rhys ^^ So, in code playground, I correct / improve / comment your version @Kevin: https://code.sololearn.com/clKlAuiLUF38 ( 'guess the link work, as I don't mark the code as public: if not, say it, I'll made it public )
8th Jan 2017, 3:11 AM
visph
visph - avatar
+ 2
@richard: Check my last version of improvments ( linked in previous post ): I already improve user input to be case insensitie ( using method title() ), and accept number as well as month names ;)
9th Jan 2017, 5:01 AM
visph
visph - avatar
+ 1
Hey! Thanks for the swift replies. Ah dictionaries! Very nice idea. Also, very good questions about 'Chwefror' appearing three times there. That's the aftermath of my copying and pasting. All in all I think two things have popped up here. First of all I need to look over my work thouroughly and also I need to keep at it with my problem-solving skills. Back to the drawing boardd. Thanks guys!
7th Jan 2017, 8:31 PM
Rhys Williams
Rhys Williams - avatar
+ 1
# Hello Rhys a translator is a great idea for a program. # there are couple of online tools that you could # use to improve your code. # A quick format makes it easier to read # There is an online tool for Python2 # http://pythoniter.appspot.com/ # (if you use it just reinsert any print brackets manually into your code.) # It's a lot easier to use than Pylint or other # commanline tools when you are learning. # There is also a tool to help # general complience to python PEP standards # http://pep8online.com/ # Otherwise If you want You could drop # the function and access the # dictionary directly .get() method that allows you # to specify a default return value if the key # is missing. # Don't worry about data type errors on the input it is # always type string. # Since you have spent a lot of time typing # in all the translation data if you use .capatalize() # on the user input it will be standardized to match # your dictionary key string format regardless of what # anyone types in. Hope the suggestions help you, Well done . translated = { 'January': 'Ionawr', 'February': 'Chwefror', 'March': 'Mawrth', 'April': 'Ebrill', 'May': 'Mai', 'June': 'Mehefin', 'July': 'Gorffennaf', 'August': 'Awst', 'September': 'Medi', 'October': 'Hydref', 'November': 'Tachwedd', 'December': 'Rhagfyr', } input_month = \ input("Please enter your birth month \ (January, February, etc.) or 'Quit' to exit: " ).capitalize() if input_month != 'Quit': print('Your birth month (' + input_month + ")\ in Welsh is: " \ + translated.get(input_month,"Unknown")) else: print('Quitting...')
9th Jan 2017, 4:54 AM
richard
0
Ah nice, see my next question was going to involve adding a little more complexity to the program. I like the error-checking and quitting dialogue as further expansions. I'll update the code tomorrow and see what you all think. You guys are great!
9th Jan 2017, 10:07 PM
Rhys Williams
Rhys Williams - avatar