我创建了一个 python 刽子手游戏,除了一个与计算失败尝试次数相关的逻辑错误外,它运行良好。
failed = 0
for char in word:
if char in letter_guess:
print(char, end="")
if letter_guess == word:
print("")
print('CONGRATULATIONS! YOU WON')
sys.exit()
else:
failed = failed + 1
print('_ ', end="")
if failed == 15:
print("GAME OVER! your word was", word)
sys.exit()
当玩家猜错一个字母时,它不是只添加一个,而是为每个不是玩家猜到的字母的字母添加一个。例如,如果单词是“star”,而玩家猜到了字母“e”,那么程序将添加 4 表示失败;每个字母一个。我不知道如何解决这个问题,以便它只执行一次这个特定的功能,因为我仍然需要它为每个错误的字母添加一个“_”。我想也许我可以将 4 个字母单词的尝试次数乘以 4,但我不确定这是否有效。
这是完整的代码
import random
import time
import sys
# GAME INTRODUCTION
print('HANGMAN')
name = input('Username: ')
print('Welcome', name, 'are you ready to play?')
answer = ''
print("type 'start' to begin")
while answer != 'start':
time.sleep(0.7)
answer = input()
print("OK! Let's begin")
print("pick the mode/difficulty of the game")
time.sleep(0.7)
print("1-Easy")
print("2-Medium")
print("3-Difficult")
# POSSIBLE WORDS IN GAME
words_4 = ['time', 'king', 'song', 'disk', 'meal',
'cell', 'hair', 'menu', 'math']
words_5 = ['world', 'paper', 'hotel', 'queen', 'uncle',
'night', 'hotel', 'shirt', 'pizza']
words_6 = ['person', 'tennis', 'camera', 'sector',
'potato', 'safety', 'growth', 'thanks']
# CHOOSING GAME DIFFICULTY
mode = str(input())
if mode in ['Easy', '1', 'easy']:
word_list = words_4
letter_num = 4
print("your word consists of 4 letters")
elif mode in ['Medium', '2', 'medium']:
word_list = words_5
letter_num = 5
print("your word consists of 5 letters")
elif mode in ['Difficult', '3', 'difficult']:
word_list = words_6
letter_num = 6
print("your word consists of 6 letters")
else:
print("Mode does not exist!")
慕侠2389804
蓝山帝景
婷婷同学_
相关分类