如何为Python版Hangman游戏添加重玩功能及胜负占比统计
Hangman游戏优化:添加重玩功能与胜率统计
需求说明
已完成Hangman游戏核心逻辑,hangman()函数可正常运行并返回"victory"或"defeat"。现需实现:
- 游戏结束后询问玩家是否重玩
- 最终展示胜利、失败的百分比
- 保留
hangman()函数返回值特性
修改后完整代码
def hangmandrawing(index): tab = [''' +---+ | | | | | | =========''', ''' +---+ | | O | | | | =========''', ''' +---+ | | O | | | | | =========''', ''' +---+ | | O | /| | | | =========''', ''' +---+ | | O | /|\ | | | =========''', ''' +---+ | | O | /|\ | / | | | =========''', ''' +---+ O | /|\ | / \ | | | ========='''] return tab[index] import random words = ["critic","modest","crystal","vision","block"] def hangman(): # 移除硬编码,恢复随机选词 mystery_word = random.choice(words) word_found = list(mystery_word) print("This is the state of the hangman : \n " ,hangmandrawing(0), " \n") print(f"The mystery word is : {len(mystery_word) * ' _' } and has {len(mystery_word)} letters. \n ") lives = 6 drawing_number = 1 hangman.letters = [] while (''.join(word_found)) != mystery_word or lives > 0: l = input("Enter a letter : ").lower() if len(l) > 1 or len(l) == 0: print("You have to enter 1 letter.\n") continue elif l in hangman.letters: print("You have already entered this letter.\n") continue hangman.letters.append(l) word_update(mystery_word, word_found, l) print(f"\n{word_update.answer}") if l in mystery_word: times_l = (mystery_word).count(l) print(f"Well done! The letter you have entered is {times_l} time{'s' if times_l>1 else ''} in the word. ") print(f"The word is now : {''.join(word_found)}\n") elif l not in mystery_word: lives -= 1 print(f"The letter you have entered is not in the word, you now have {lives} lives. \n") print("This is the state of the hangman : " ,hangmandrawing(drawing_number), "\n") print(f"The word is still : {''.join(word_found)}\n") drawing_number += 1 if (''.join(word_found)).replace(" ", "") == mystery_word: # 修正拼写错误foudn为found print("Well done! You have found the word and won the game.\n") return "victory" elif lives == 0: print("Unlucky, you have no more lives and you have lost.") print(f"The word was {mystery_word}. \n") return "defeat" print(f"You have used the letters : ({', '.join(sorted(hangman.letters))})\n") def word_update(mystery_word, word_found, l): for i in range(len(mystery_word)): if list(mystery_word)[i] in hangman.letters: word_found[i] = " " + list(mystery_word)[i] else: word_found[i] = " _" if l in mystery_word: word_update.answer = True return word_found, word_update.answer elif l not in mystery_word: word_update.answer = False return word_found, word_update.answer # 新增游戏主循环与统计逻辑 def main(): total_games = 0 victories = 0 defeats = 0 while True: result = hangman() total_games += 1 if result == "victory": victories += 1 else: defeats += 1 # 询问是否重玩 play_again = input("Do you want to play again? (y/n): ").strip().lower() while play_again not in ['y', 'n']: play_again = input("Please enter 'y' (yes) or 'n' (no): ").strip().lower() if play_again == 'n': break # 计算并展示百分比 if total_games > 0: victory_percent = (victories / total_games) * 100 defeat_percent = (defeats / total_games) * 100 print("\n--- Game Statistics ---") print(f"Total games played: {total_games}") print(f"Victories: {victories} ({victory_percent:.1f}%)") print(f"Defeats: {defeats} ({defeat_percent:.1f}%)") if __name__ == "__main__": main()
关键优化说明
- 主循环逻辑:新增
main()函数作为游戏入口,维护total_games、victories、defeats三个统计变量,循环调用hangman()函数 - 重玩询问:每次游戏结束后获取玩家输入,仅接受'y'/'n'有效选项,控制循环是否继续
- 百分比展示:玩家退出后,基于统计数据计算胜率和败率,保留1位小数输出
- 细节修复:移除
mystery_word的硬编码,修正拼写错误,添加字母出现次数的复数处理
内容的提问来源于stack exchange,提问作者nickipriv
相关产品推荐
相关产品推荐

