简体   繁体   中英

why is my while loop running print _ forever?

I think the problem is with my second while loop. It keeps printing the _ after I press enter to run the game. I'm not sure how the condition isn't being met or where to add a break. I have tried changing the indentation of my else: and when I do the loop won't run at all.

import random

#make a list of words
words = [
  'apple',
  'banana',
  'orange',
  'coconut',
  'strawberry',
  'lime',
  'grapefruit',
  'lemon',
  'kiwi',
  'blueberry',
  'melon'
]

while True:
  start = input("Press enter/return to start, or Q to quit")
  if start.lower() == 'q':
    break

  #pick rand word
  secret_word = random.choice(words)
  bad_guesses = []
  good_guesses = []

  while len(bad_guesses) < 7 and len(good_guesses) != len(list(secret_word)):
    #draw guessed letters, spaces, and strikes
    for letter in secret_word:
      if letter in good_guesses:
        print(letter, end='')
      else:
        print('_', end='')


  print('')
  print('Strikes: {}/7'.format(len(bad_guesses)))
  print('')

  #take guess
  guess = input("Guess a letter: ").lower()

  if len(guess) != 1:
    print("You can only guess a single letter!")
    continue
  elif guess in bad_guesses or guess in good_guesses:
    print("You already guessed that letter!")
    continue
  elif not guess.isalpha():
    print("You can only guess letters!")
    continue

  if guess in secret_word:
    good_guesses.append(guess)
    if len(good_guesses) == len(list(secret_word)):
      print("You Win! The word was {}".format(secret_word))
      break
  else:
     bad_guesses.append(guess)
else:
  print("You didn't get it! My secret word was {}".format(secret_word))

In the block with this condition:

while len(bad_guesses) < 7 and len(good_guesses) != len(list(secret_word)):

nothing changes about the bad_guesses , good_guesses , or secret_word variables. That means you'll just keep retrying it forever.

You probably wanted an if not a while in that case.

Here is a working version of your code. Just corrected the necessary parts, but undoubtedly you should consider writing a more clean, and better indented code.

import random

# make a list of words
words = [
      'apple',
      'banana',
      'orange',
      'coconut',
      'strawberry',
      'lime',
      'grapefruit',
      'lemon',
      'kiwi',
      'blueberry',
      'melon'
]

while True:
    start = input("Press enter/return to start, or Q to quit")
    if start.lower() == 'q':
        break

    bad_guesses = []
    good_guesses = []

    # pick rand word
    secret_word = random.choice(words)

    while len(bad_guesses) < 7 and len(good_guesses) != len(list(secret_word)):
        # draw guessed letters, spaces, and strikes
        for letter in secret_word:
            if letter in good_guesses:
                print(letter, end='')
            else:
                print('_', end='')

        print('\Bad guesses: {}/7\n'.format(len(bad_guesses)))

        # take guess
        guess = input("Guess a letter: ").lower()

        if len(guess) != 1:
            print("You can only guess a single letter!")
            continue
        elif guess in bad_guesses or guess in good_guesses:
            print("You already guessed that letter!")
            continue
        elif not guess.isalpha():
            print("You can only guess letters!")
            continue

        if guess in secret_word:
            good_guesses.append(guess)
        else:
            bad_guesses.append(guess)

        if len(good_guesses) == len(set(secret_word)):
            print("You Win! The word was {}".format(secret_word))
            break

print("You didn't get it! My secret word was {}".format(secret_word))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM