二手产品经理

二手产品经理

THIS IS RENO

综合练习 - 39天 - 在线学python100天

记录#

  1. 学习对字典的操作。
  2. random.choice () 随机返回字典中的一个值。
  3. 今天的练习是猜单词游戏,5 次机会猜中一个单词。
  4. 今天的练习很烧脑,没写出来,抄袭正确答案,大概看明白了。
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")


加载中...
此文章数据所有权由区块链加密技术和智能合约保障仅归创作者所有。