在刽子手游戏Python中重复的单词

时间:2015-01-04 05:42:26

标签: python-3.x pygame

我有一个问题,当在Hangman游戏中有一个像happy这样的词时,它只会附加1' p'在列表中...运行我的代码,请告诉我该怎么做? 检查我的循环。

import random
import time
File=open("Dict.txt",'r')
Data = File.read()
Word = Data.split("\n")

A = random.randint(0,len(Word)-1)

Dict = Word[A]

print(Dict)
Dash = []
print("\n\n\t\t\t","_ "*len(Dict),"\n\n")
i = 0
while i < len(Dict):

    letter = str(input("\n\nEnter an alphabet: "))
    if letter == "" or letter not in 'abcdefghijklmnopqrstuvwxyz' or len(letter) != 1:
        print("\n\n\t\tPlease Enter Some valid thing\n\n")
        time.sleep(2)
        i = i - 1

    if letter in Dict:
        Dash.append(letter)
    else:
        print("This is not in the word")
        i = i - 1


    for item in Dict:
        if item in Dash:
            print(item, end = " ")
        else:
            print("_", end = " ")

    i = i + 1

1 个答案:

答案 0 :(得分:1)

错误在于第25行的“中断”:一旦你用字母“p”填充了一个空格,循环就会中断并且不会用“p”填充第二个空格。

您需要有一个标志变量来记住是否已成功填写任何空格,如下所示:

success = False
for c in range(len(Dict)):
  if x == Dict[c]:
    Dash[c] = x
    success = True
if not success:
  Lives -= 1

P.S。您发布的代码缩进有问题。