0

Please please help me

Can you please help me fix this code https://sololearn.com/compiler-playground/cSwMvA117WGa/?ref=app

5th Feb 2024, 1:48 PM
Theo
Theo - avatar
7 odpowiedzi
+ 2
Theo , Lothar 's refactoring to a dict lookup is a good way if you're ready to learn dict usage, but if you just want to fix your existing code, change all your comparisons to check the input against a literal, not a variable. In other words, change this, if es == a: print(a) to this. if es == "a": print(a) And the rest too. And change the directions to tell the user to enter a letter, not a word, because your program tells how to pronounce individual letters, not whole words. Please also add a 'python' tag.
5th Feb 2024, 10:38 PM
Rain
Rain - avatar
+ 8
Theo , Rain , an additional comment from me is *not* to use 26 independent `if conditionals`, but to use a conditional construction like: if ...: ... elif...: .... elif ...: ... else: > by using independent `if conditionals` python will always check *all* conditionals, even if the required ones was found already and the output has been printed. > using `if... elif ... else` is skipping the remaining conditions when the required ones is found. the else clause can be used to output a message if no match is found.
6th Feb 2024, 10:07 AM
Lothar
Lothar - avatar
+ 5
Theo , 🌀NB🌀 , the code as it is, is *not working for input words*, but only for single letters. if we wanted to use it with words, we need a loop to iterate over the input word. an other issue is the code itself how it is structured: > a lot of independent variables are used for each letter that can occur. to evaluate this, a lot of `if` conditionals are used. it would be better to use a dictionary (which is one variable), that can handle the pairs of letters and how to pronounce it. this will make the code much shorter, easier to maintain and easier to read. could be done like: (add the missing letters) letters = { 'a': 'ay', 'b': 'bee', 'c': 'see', 'd': 'dee', 'e': 'ee', 'f': 'ef' } letter = input() print(letters.get(letter, 'letter not found'))
5th Feb 2024, 4:19 PM
Lothar
Lothar - avatar
+ 3
Theo in addition to the answer Rain I would like to suggest using "elif" instead of repeating "if" commands, this will make the conditional branching a single whole, that is, when you enter, for example, "a", the first condition will work and code execution will stop there. if es == 'a': print(a) elif es == 'b': print(b) elif ... In your case, there are many conditional branches, and no matter which letter is entered, they will always be checked all the way to the end.
6th Feb 2024, 10:07 AM
Solo
Solo - avatar
+ 3
Lothar , are you rewriting my answers? Funny...🤣🤣🤣
6th Feb 2024, 7:31 PM
Solo
Solo - avatar
+ 1
I am a starter I need a wonderful mentor
6th Feb 2024, 4:25 PM
Onwuagbaizu Sunday
Onwuagbaizu Sunday - avatar