二手产品经理

二手产品经理

THIS IS RENO

Comprehensive Practice - 39 Days - Learn Python Online for 100 Days

Record#

  1. Learn how to manipulate dictionaries.
  2. random.choice() randomly returns a value from the dictionary.
  3. Today's exercise is a word guessing game with 5 chances to guess a word.
  4. Today's exercise was challenging, couldn't solve it, copied the correct answer, and roughly understood it.
import random, os, time

listOfWords = ["apple", "orange", "grapes", "pear"]
letterPicked = []
lives = 6

word = random.choice(listOfWords)

while True:
  time.sleep(1)
  os.system("clear")
  letter = input("Choose a letter: ").lower()
  
  if letter in letterPicked:
    print("You've tried that before")
    continue
    
  letterPicked.append(letter)
  
  if letter in word:
    print("You found a letter")
  else:
    print("Nope, not in there")
    lives -= 1
  
  allLetters = True
  print()
  for i in word:
    if i in letterPicked:
      print(i, end="")
    else:
      print("_", end="")
      allLetters = False
  print()

  if allLetters:
    print(f"You won with {lives} left!")
    break

  if lives<=0:
    print(f"You ran out of lives! The answer was {word}")
    break
  else:
    print(f"Only {lives} left")


Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.