hangman 游戏中的重复单词 Python
Duplicate word in hangman game Python
我有一个问题,当 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
第 25 行的 "break" 出现错误:一旦您用字母 "p" 填写了一个 space,循环就会中断,不会填写第二个 space 与 "p".
你需要有一个标志变量来记住是否有任何space被成功填写,像这样:
success = False
for c in range(len(Dict)):
if x == Dict[c]:
Dash[c] = x
success = True
if not success:
Lives -= 1
P.S。您发布的代码的缩进有问题。
我有一个问题,当 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
第 25 行的 "break" 出现错误:一旦您用字母 "p" 填写了一个 space,循环就会中断,不会填写第二个 space 与 "p".
你需要有一个标志变量来记住是否有任何space被成功填写,像这样:
success = False
for c in range(len(Dict)):
if x == Dict[c]:
Dash[c] = x
success = True
if not success:
Lives -= 1
P.S。您发布的代码的缩进有问题。