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