+ 2
[SOLVED] Isogram Detector
Why my 2nd IF statement does not work? I do know that some people might recommend using set() but i feel like doing it in 'hard way' as a beginner as i kinda weak in reading for loop. Many thanks in advance. https://sololearn.com/compiler-playground/cFXRjnC8XzO7/?ref=app
17 Antworten
+ 8
SeaWater ,
(1) i would suggest to check the complete input word for isalpha() rather then check each individual character in the loop.
also handle the case if there are *non alphabetical characters.*
(if you are reffering to the *isogram* code coach in the community section, we do not need to check for isalpha())
(2) better to rename the var *tmpwordlist* to *tempcharlist*, because we have to check and append a character not the input word.
(3) check if the current character is in the var *tempcharlist*.
if yes: character is duplicated, so no isogram, break the loop.
if no: append it there.
(4) to get the correct output, we can use a status variable.
+ 8
SeaWater ,
your code is quite good now, but needs to do also an output if an isogram is detected.
what we can also use instead of the *status variable* is an *else clause* with the for loop. this is a more pytonic way and will work like described here:
"The else clause with loops is executed when the loop completes all its iterations without encountering a break statement. This implies that the loop has naturally exhausted its entire iterable. If the loop is prematurely exited using break, the else block will be skipped."
this means we can do the output (for the case that the input is an isogram) in this else clause like:
for ...
...
else:
# do output here
+ 7
SeaWater ,
you can find here some more use cases about using *else* with loops:
https://machinelearningtutorials.org/JUMP_LINK__&&__python__&&__JUMP_LINK-else-clauses-on-loops-with-examples/
+ 6
JaScript ,
using your suggested code gives these results:
> input: 'abc'
output::
true
true
true
> input: 'abb'
true
true
false
the output should be 'true' if input word is an isogram, or 'false' if word is not an isogram.
+ 3
I don‘t know this task „isogram detector“ but my understanding of the code looks like follows:
#new code
word = str(input()).lower()
tempcharlist = []
for letter in word:
if letter in tempcharlist:
print ('false')
break
else: #if there is nothing more to be checked and nothing more to be added to the temporary list this will be executed
tempcharlist.append(letter)
print('true')
+ 3
input() is str by default. no need for str(input())
I posted an alternative solution as a comment in your code. you can use the count method to check if a letter occurs more than once.
I also used the walrus operator := to reduce the number of lines in the code. It is not strictly necessary but it is good to learn additional things. I think all this code can be distilled into one print call, but this if for future you to figure out.
+ 2
Lothar,
Thanks for the input now i know my 2nd IF did not working because of that isalpha() return i think and yes it is indeed from Isogram Code Coach. Also what is the status variable? is it simple variable like this:
conditionsuccess = 'true'
or other than that?
I did try searching it through internet but all i could found is SQL Status Variable
If i were right about that status variable, does that mean you are also indirectly suggesting me using function intead of explicitly printing the condition? Because i also kinda stucked how to print the true condition, if i using a function i could easily using,
return 'true'
+ 2
we often learn more by making mistakes and going down the wrong path. That's the fun part.
+ 1
Lothar,
neat, didn't know i could use else statement with for loops i thought it was only for IF statement. Thanks for that part, glad i'm doing it the 'hard way' .
+ 1
Bob_Li
yes my bad there for converting input to string again where its already string in the first place.
I do try that oneliner of yours but modified it a bit to my taste as i still find lambda little bit intimidating for me.
Appreciated that one with count method though. I never thought iterations could be done like that too.
+ 1
Solo,
it does on the newer code though? what i know is isalpha() make that code did not work for sure as i also tried bundled it before using AND in the same IF and it's still not working.
and for the end note i guess you are right we only need to compare it's len(), tbh before implementing this for loop my brain wander too far away as i thought i need to compare each character but in the end i scrapped it because 'How the heck im gonna put it into code and how is that even gonna work. ' situation so thats why lower() is still left there. Perks of a noob who still learning how to code and problem solving i guess, overcomplicating things where it wouldn't work or you won't be able to make it either. 😑
0
JaScript
It's from Community Code Coach, its considered in easy category though
0
Bonjour
0
SeaWater the second condition does not work because you are trying to search for a letter in a list of words...😎
P.S: "to solve this problem, you do not need to use the lower() method."
0
Hi
0
Hi