Advertisement
acclivity

pyWordGuessingGame

Apr 21st, 2022 (edited)
1,076
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.56 KB | None | 0 0
  1. # Word Guessing Game
  2.  
  3. # (This is a simplistic version for assistance/demo purposes. in practice there would be more aspects included in the script)
  4.  
  5. word = "bubble"
  6. wlist = list(word.lower())          # Make a list from the word for easier processing
  7. displaylist = ["-"] * len(word)     # Make a list of the user's guesses so far (list is mutable, string is not)
  8. tries = 4                           # This is one more than the number of bad guesses allowed
  9.  
  10. while tries:                        # Loop while still some bad guesses remain
  11.     print(*displaylist, sep="")     # Display the word as guessed so far
  12.     guess = input("Guess a letter in the word: ").lower()
  13.     if guess in wlist:
  14.         print("Correct letter!")
  15.         for x, c in enumerate(wlist):       # Search for all possible instances of this guessed letter
  16.             if c == guess:
  17.                 # we use a list for the display word, as characters can be changed directly in the list
  18.                 displaylist[x] = guess      # Put the guessed letter into the display list
  19.         if displaylist == wlist:            # Has the whole word been guessed?
  20.             print("You won! The word was", word)               # - yes - congratulate user and ...
  21.             break                           # ... break out of loop
  22.     else:
  23.         tries -= 1                          # Count 1 bad guess
  24.         print("Wrong guess. You have", tries - 1, "wrong attempts left")
  25.  
  26. else:           # the loop did NOT end with break, therefore the word was not found
  27.     print("You failed to guess the word")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement