0
Please please help me
Can you please help me fix this code https://sololearn.com/compiler-playground/cSwMvA117WGa/?ref=app
7 Respostas
+ 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.
+ 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.
+ 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'))
+ 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.
+ 3
Lothar , are you rewriting my answers?
Funny...🤣🤣🤣
+ 1
I am a starter I need a wonderful mentor