Python Python Sequences Sequence Operations Membership Testing

Itsa Snake
Itsa Snake
3,848 Points

Using while correctly to continuously loop?

Hello, what would be the correct way to use 'while' in this sample code so that the user is able to continuously search?

The below works, but i highly doubt using an 'a' variable in the way i have is the 'correct' way of doing this.

docs = 'Tuples are immutable sequences, typically used to store collections of heterogeneous data (such as the 2-tuples produced by the enumerate() built-in). Tuples are also used for cases where an immutable sequence of homogeneous data is needed (such as allowing storage in a set or dict instance).'

a = 0
while(a==0):
  user_word = input("Enter a random word: ")

  if user_word in docs:
    print(f"The word '{user_word}' is within the variable docs")
  else:
    print(f"the word '{user_word}' is not present")

3 Answers

Cameron Sprague
Cameron Sprague
11,246 Points

Hi Itsa,

I believe I see how you are trying to use a while loop, and we call it while True. While true is tricky because it creates an infinite loop. It's ok to create an endless loop once in a while because that is how we learn. There are lots of other ways to use a while loop. I can send you a problem to solve with a while loop if you are interested in giving it a try. Just let me know. To answer your question above, please see the following code.

docs = 'Tuples are immutable sequences, typically used to store collections of heterogeneous data (such as the 2-tuples produced by the enumerate() built-in). Tuples are also used for cases where an immutable sequence of homogeneous data is needed (such as allowing storage in a set or dict instance).'
while True:
    print('\n'+'Enter exit to exit the program')
    user_word = input('Enter a random word: ')
    if user_word == 'exit':
        print('Exiting the program')
        break
    elif user_word in docs:
        print('The word ' + user_word + ' is within the variable docs')
    else:
        print('the word ' + user_word + ' is not present')

It is a good idea to accompany while True with a break. Break allows a user to exit a while loop or program. You can see this in the line "if user_word == 'exit':" I am testing to see if the user has entered exit if they have then print exiting the program and break out of the loop. Please let me know if you have any questions about the code above.

As an extra point. Usually, I convert user input to uppercase or lowercase when I am checking it against values. We do not know if the user will type exit, EXIT, or eXiT. We can use either string methods of .upper or .lower. Please see the use of .lower in the code below, where I convert all of variable docs into lowercase and our user input into lowercase. Please let me know if you have any questions about the code below.

docs = 'Tuples are immutable sequences, typically used to store collections of heterogeneous data (such as the 2-tuples produced by the enumerate() built-in). Tuples are also used for cases where an immutable sequence of homogeneous data is needed (such as allowing storage in a set or dict instance).'
docs = docs.lower()
while True:
    print('\n'+'Enter exit to exit the program')
    user_word = (input('Enter a random word: '))
    user_word = user_word.lower()
    if user_word == 'exit':
        print('Exiting the program')
        break
    elif user_word in docs:
        print('The word ' + user_word + ' is within the variable docs')
    else:
        print('the word ' + user_word + ' is not present')
Itsa Snake
Itsa Snake
3,848 Points

Thanks so much Cameron. That's helpful and brings back memories of a much earlier lesson.

As I have progressed until this stage and plan to finish all the lessons.. where you suggest I start to actually practice coding, i.e. not within workspace on treehouse! There seem to be so many different platforms like Django but I wondered where real coders usually progress to after learning the theory

I'd hardly be called a real coder (not now, anyway - that was 30 years ago!) but I find Visual Studio Code easy and helpful. It runs on my Linux and Chromebook machines, and I assume Windows too.

justlevy
justlevy
1,995 Points

replit is my jam. Definitely check it out. It's a simple, web-based IDE.

justlevy
justlevy
1,995 Points

Cameron Sprague - thanks for sharing that code example.

Question - why does entering 'tu' return true ('The word tu is within the variable docs'). In other words, how can we make the program return true if the entire word matches, versus a single letter?