user1679073
user1679073

Reputation: 47

Python - my while loop is stopping itself

This is a homework question and it asks "Pick a word. Ask the user to enter a letter. Check how many times the letter is present in your word and output the number on the screen." So far I have written it as is and it seems to be working fine:

word = str("python")
letters = len(word)
attempt = str(raw_input ("Enter a letter: "))

while attempt in word:
    count = "python".count(attempt)
    if attempt in word:
                    print "This letter is present ",count, "time(s)."
    attempt = str(raw_input ("Enter another letter: "))

while attempt not in word:
    attempt = str(raw_input ("This letter is not present, enter another letter: "))
    count = "python".count(attempt)
    if attempt in word:
             print "This letter is present ",count, "time(s)."

but occasionally if I am inputting letters the program will stop and no longer take any more inputs. What am I doing wrong? I apologize if the code is very sloppy and poorly written as I am new to programming. Thanks.

Upvotes: 0

Views: 977

Answers (2)

BrenBarn
BrenBarn

Reputation: 251568

I'm not sure if this is what you're getting at, but your while loops will execute one after the other. They don't both apply all the time. So the first while means "keep doing this as long as the user enters a letter that is in the word", and that loop will keep executing as long as the user enters letters in the word. As soon as the user enters one letter that isn't in the word, the loop will end and things will move on to the second loop. Execution will never go back to the first loop. Likewise, once in the second loop, if you then enter a letter that is in the word, the loop will end. Since that's the end of the program, the program will end.

So if you enter a letter that isn't in the word, then enter a letter that is in the word, the program will end. Like if you first enter "x" and then enter "y", it will then stop.

I think what you really want is more like:

while True:
    attempt = raw_input("Enter a letter:")
    if attempt in word:
        print "That was in the word", word.count(attempt), "times"
    else:
        print "That was not in the word"

Of course, this program will loop infinitely until you close it by pressing Ctrl-Break or the like.

There are some other issues with your code. You don't need to wrap "python" in str, since that already is a string. You also don't need to wrap raw_input in a string, since raw_input already returns a string. You define a variable called letters but never use it.

Also, you define word = "python" at the beginning, but then sometimes later you use the word variable, while other times you retype the string "python" in your code. It doesn't matter for this program, but in general it's a good idea to assing the variable once and use that everywhere; otherwise you'll have to change it in many places if you decide to use a different word, which increases the likelihood that you'll forget to change it in one place, and thereby cause a bug.

Finally, note that in and count operate on substrings, not just single letters. So entering "yth" as as your input will still work and give a count of 1. There's not necessarily anything wrong with this, but you should be aware that although you're asking for letters the person can type in anything, and substrings of any length will still be found in the "word".

Upvotes: 5

user684934
user684934

Reputation:

The program is sequential with two loops. Once those loops have been passed the program ends.

Loop 1: run while the input is found in the word.

As soon as this condition fails, we fall through to loop 2:

Loop 2: run while the input is not found in the word.

As soon as this condition fails, we fall through to the end of the program.

So, if you enter a "bad" input once, then a "good" input once, the program will end.

What you want to do is wrap both loops into one, and use an if-else to decide which each particular input is.

Upvotes: 0

Related Questions