Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Word Guessing Game
- # (This is a simplistic version for assistance/demo purposes. in practice there would be more aspects included in the script)
- word = "bubble"
- wlist = list(word.lower()) # Make a list from the word for easier processing
- displaylist = ["-"] * len(word) # Make a list of the user's guesses so far (list is mutable, string is not)
- tries = 4 # This is one more than the number of bad guesses allowed
- while tries: # Loop while still some bad guesses remain
- print(*displaylist, sep="") # Display the word as guessed so far
- guess = input("Guess a letter in the word: ").lower()
- if guess in wlist:
- print("Correct letter!")
- for x, c in enumerate(wlist): # Search for all possible instances of this guessed letter
- if c == guess:
- # we use a list for the display word, as characters can be changed directly in the list
- displaylist[x] = guess # Put the guessed letter into the display list
- if displaylist == wlist: # Has the whole word been guessed?
- print("You won! The word was", word) # - yes - congratulate user and ...
- break # ... break out of loop
- else:
- tries -= 1 # Count 1 bad guess
- print("Wrong guess. You have", tries - 1, "wrong attempts left")
- else: # the loop did NOT end with break, therefore the word was not found
- print("You failed to guess the word")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement