-3

The code is supposed to take any string input and check if the word is an isogram (word that does not use repeating letters). But it doesn't actually do that sadly.

# word input and defining variables
word = list(str(input()))
letter = 0
letters = len(word)
x = 0

while letter <= letters: # while loop to repeat once for each letter
    if word.count([letter]) > 1: # checks if the letter is repeated more than once
        x += 1
        letter += 1 # letter is raised by one so it moves onto the next place in the list
    else:
        letter += 1

# printing result
if x == 0:
    print("true")
else:
    print("false")
Dim
  • 1
  • 2
  • `word` does not contain the list `[letter]` therefore `x += 1` is never executed. – mkrieger1 Nov 03 '22 at 14:20
  • also note that "letter" is actually the index of a letter, not the letter itself. Maybe you wanted word[letter] – Kenny Ostrom Nov 03 '22 at 14:30
  • In general, an interactive [debugger](/q/25385173/90527) is your most powerful tool in cases like this, for troubleshooting unexpected behavior and crashes. Find and learn to use whatever debugger your development suite provides. Please look over the [help], especially the "[ask]" article. See also "[How much research effort is expected of Stack Overflow users?](//meta.stackoverflow.com/q/261592/90527)" – outis Nov 04 '22 at 09:34

2 Answers2

0

letter as you've defined it is a number. word.count(<substring>) will count how many times <substring> appears in word. Why would you count how many time the number you are storing in letter appear in word?

Instead, loop through every letter in word and test word.count(letter). If it's greater than one, then set a flag variable that stores whether this is an isogram or not, and break the loop since we have discovered at this point that it's not an isogram.

word = input()

isogram = True
for letter in word:
    if word.count(letter) > 1: 
        isogram = False       
        break
    

if isogram:
    print("true")
else:
    print("false")

Granted, there are more elegant ways to solve this and it could be code-golfed to death, but I think this is what you were originally aiming for before your lost your way with counter variables and lists.

JNevill
  • 46,980
  • 4
  • 38
  • 63
  • Got it. That makes sense. `word.count(word[letter])` would have gotten you there (probably), but I think it's over complicated for your needs (unless the homework assignment was specific about using a while loop here where you may need to juggle a couple of counting variables or something). – JNevill Nov 03 '22 at 14:34
  • Just updated the answer to remove the `list(str())` bit when collecting your input. `input` already returns a string so no need to cast it. Also there is no need to convert to a list as `for letter in word` will already iterate through every character in the string. – JNevill Nov 03 '22 at 14:35
-1

Why not just count the number of unique letters in your word and compare it with total number of letters in the word with something like

word = str(input())
if len(set(word)) == len(word):
    print("true")
else:
    print("false")
Robin Nicole
  • 646
  • 4
  • 17
  • Well to be completely honest I have no idea what `if len(set(word))` does. What does `set(word)` mean? – Dim Nov 03 '22 at 14:54