+ 2
Coding Coach - It’s a Sign
Hello, I have found how the “open” string works but all the “trash” strings are skipping with this code: i = 0 for i in range(4): sign_word = input().split() res = 0 if sign_word == sign_word[::-1]: res = 1 if res == 1: string = "Open" else: string = "Trash" print(string) Could someone give me a suggestion on what error I have in this? Thank you https://sololearn.com/coach/19/?ref=app
24 odpowiedzi
+ 6
Benjamin A Keller , Emerson Prado ,
the variable name '_' is a regular name for identifiers in python. but it has a special meaning. it indicates that this variable will not been used in the code, we only need it (in this case) to run a for loop. (throwaway variable)
but it differs not from other variables, it holds the values that are assigned to it, there is no memory saving given.
words = ['hello', 'airport hotel']
for _ in words:
print(_)
print(_.upper())
+ 5
Benjamin A Keller 2 corrections and 2 improvements:
The comparison with sign_word expects it to be a string. But the split function makes it a single element list.
The code should output"Open" if any word is a palindrome. But you always test all words and output the result of the last one. You need to stop when it finds a palindrome.
You don't need to initialize a loop variable which is assigned values in the loop itself.
The "res" variable is not needed. See how the 2 "if"s test exactly the same thing.
+ 5
Benjamin A Keller,
see some hints and a basic code sample in the attached file.
https://code.sololearn.com/cypt0S6b6ile/?ref=app
+ 5
vikram jeet Sharma ,
i am not sure what you mean with your description of an error when using split().
i personally could not find a problem using split(), even if we typed a word, a letter, or a space, or nothing at all.2
maybe you can make a short sample of code and various input values for us?
+ 3
Dragon RB Pls avoid giving finished code as answer, because it makes the OP skip the most important part of learning. Prefer giving hints for the OP to find the solution instead.
+ 2
It depends on what you want from this code. Pls include the task description in the question description.
Also, pls save your code in Code Playground and include a link to it in the question description. This way, we can run the code and avoid copying errors.
+ 2
vikram jeet Sharma Pls don't give finished code as answer, because it makes the OP to skip the most important part of learning process. Always prefer giving hints for the OP to find the solution.
That said, note that when you call split on a string, it returns a list. So the first "if" in your code creates an inconsistency: sign_word can have a string or a list, depending on text input.
+ 1
I have a feeling the first if statement or the res = 0 initiative might not be necessary?
+ 1
Benjamin A Keller You're in the right direction. Now you just have to loop though all the words and adjust the output logic accordingly.
+ 1
Lothar may I use my own words still?
+ 1
Benjamin A Keller You should always use your own words. That's what makes you learn.
+ 1
Thank you - and i changed changed my string declaration to “decision”
+ 1
And its solved!
+ 1
I do have one question - why in the for loop this suggest for _(underscore) in range of(4)?
+ 1
Underscore is a variable which doesn't keep its value. Since you never need it, this saves memory.
Even if it's just a little, it's always good to adopt good practices.
+ 1
Emerson is it possible to apply the same underscore variable technique to other programming languages or no?
I am just curious - I’m familiar with quite a few and was wondering which techniques i could exchange across the board while dealing with syntax etc.
+ 1
Though you can always limit variables scope by using functions, methods and, depending on the language, code blocks. This way, they use memory, but get cleaned as soon as the function, etc., ends.
As a last resort, you can erase them explicitly after use, like with Python's "del" function.
+ 1
Lothar Interesting. I can remember reading about the _ variable being discarded after the loop, but your sample code proves the opposite.
+ 1
Emerson Prado I understand.