我正在为一个学校项目做一个简单的游戏。它只是猜测游戏这个词,除了答案错误或完成时,我已经把大部分内容都删掉了。到目前为止,这是我的代码。
guess = input('Guess a letter.')
blank_answer = list('_ _ _ _ _ _')
correct_answer ='banana'
correct_answer_final = 'b a n a n a'
guess_num = 4
index_num = 0
while guess_num > 0 and "".join(blank_answer) != correct_answer_final:
index_num = 0
for letter in correct_answer:
if letter == guess:
blank_answer[index_num] = letter
index_num += 2
if letter != guess:
guess_num -= 1
print('The Word so far is ' + "".join(blank_answer)
guess = input('Guess a letter. (You have ' + str(guess_num) + ' guesses remaining)')
if "".join(blank_answer) == correct_answer_final:
print('Good job ')
if guess_num == 0:
print('sorry you ran out of guesses')
我遇到的主要问题是在第一次猜测之后它会说' 3'猜测剩下然后它会在经过几次猜测之后跳到2,即使它们是正确的。在正确猜测之后,它也不会立即跳出循环。如果正确,您必须再次输入正确的字母。这是在图形窗口中完成的,因此我已将所有导入的命令更改为默认的python,以便更容易理解。任何帮助都会很棒!
答案 0 :(得分:0)
不确定这是否会解决您遇到的问题,但while
循环的条件似乎不正确。我认为你正在寻找的是
while guess_num > 0 and "".join(blank_answer) != correct_answer_final
(这里的差异是and
与or
)
这样,如果猜测结束或者猜对了正确的答案,循环就会退出。