我在这里有一个 python 程序可以解读一个单词,但我不确定特定部分发生了什么。
在下面由标题块引用和分隔的部分中,我不明白为什么将单词的“加扰”放入 while 循环中 - 如果没有循环,它不能工作吗?另外,有人可以解释一下while循环(while word:)中发生的一切吗?
import random
words = ('coffee', 'phone', 'chair', 'alarm')
word = random.choice(words)
correct = word
scramble = ""
while word: position = random.randrange(len(word)) scramble += word[position] word = word[:position] + word[(position + 1):]
print("The scrambled word is: ", scramble)
answer = input("What's your guess?: ")
def unscramble(answer):
while answer != correct and answer != "":
print("Sorry, incorrect.")
answer = input("Try again: ")
if answer == correct:
print("Good job, that is correct!")
unscramble(answer)