python
这是我写完成的代码,请问怎么做到让一个玩家赢了之后可以一直让他猜?等他猜错之后就换另一个人猜?import random
def main():
f=open("cattle.txt")
text=f.readlines()
secretWord=random.choice(text)
secretWord=secretWord.strip('\n')
count=1
wrongCount=0
wrongList=[]
player1=input("Enter your name: ")
player2=input("Enter your name: ")
instructions(player1,player2)
blanks=initializeBlanks(secretWord)
while "#" in blanks and wrongCount < 5:
if count%2==0:
player=player1
else:
player=player2
count+=1
letter=getGuess(wrongList,player)
if letter in secretWord:
blanks=fillInBlanks(secretWord,blanks,letter,player)
else:
wrongList.append(letter)
wrongCount=wrongCount+1
print("Wrong guess!")
statusReport(wrongList,wrongCount,blanks)
display(wrongCount)
if not "#" in blanks:
print(player,"Congratulations! You are the winner!")
else:
print("You are both losers.")
def instructions(p1,p2):
print(p1,p2,"I will select a secret word and give you the number of the word.")
print("You need to guess the letters in the word.")
print("Correctly guessed letters will be uesd to fill in the blank, incorrecly guessed letters will be used to build a snowman.")
print("The game will end if you successfully guess all the secret letters or you have 5 wrong guesses.")
def getGuess(wrongList,player):
letter = input(player+"Guess a letter: ")
punct="~!@#$%^&*()_+?:<>"
if letter in wrongList:
print("You have guessed this letter.")
return getGuess(wrongList,player)
ifletter in punct:
print("This guess is invaild,guess again!")
return getGuess(wrongList,player)
else:
letter=letter.lower()
return letter
def statusReport(wrongList,wrongCount,blanks):
print("Current status: ")
print("You have guessed",wrongCount,"times wrong.")
print("The letters you have guessed wrong are",wrongList)
print("The secret word now is",blanks)
def initializeBlanks(s):
blanks=["#"]*len(s)
return blanks
def fillInBlanks(secretWord,blanks,l,player):
location=secretWord.index(l)
blanks=l
print(blanks)
if not "#" in blanks:
print(player,"Congratulations! You are the winner!")
return blanks
def display(wrongCount):
grid=[[' ',' ',' '],
[' ',' ',' '],
[' ',' ',' '],
['~','~','~']]
Dict={1:,
2:,
3:,
4:,
5:}
for key in (Dict.keys()):
if key<=wrongCount:
value=Dict
row=value
col=value
item=value
grid=item
for row in grid:
for col in row:
print(col,end=" ")
print("\n")
main() 要实现让一个玩家赢了之后可以一直让他猜,等他猜错之后就换另一个人猜,可以在主函数内加入循环,让玩家可以多次游戏。具体做法是,在while循环的最后添加一个询问是否继续游戏的输入,如果玩家想继续游戏,则清空之前的状态,重新选择秘密单词,重置计数和错误列表;如果玩家不想继续游戏,则跳出循环结束游戏。
下面是修改后的代码:
import random
def main():
f=open("cattle.txt")
text=f.readlines()
secretWord=random.choice(text)
secretWord=secretWord.strip('\n')
while True:
count=1
wrongCount=0
wrongList=[]
player1=input("Enter your name: ")
player2=input("Enter your name: ")
instructions(player1,player2)
blanks=initializeBlanks(secretWord)
while "#" in blanks and wrongCount < 5:
if count%2==0:
player=player1
else:
player=player2
count+=1
letter=getGuess(wrongList,player)
if letter in secretWord:
blanks=fillInBlanks(secretWord,blanks,letter,player)
else:
wrongList.append(letter)
wrongCount=wrongCount+1
print("Wrong guess!")
statusReport(wrongList,wrongCount,blanks)
display(wrongCount)
if not "#" in blanks:
print(player,"Congratulations! You are the winner!")
else:
print("You are both losers.")
continue_game = input("Do you want to continue playing? (Y/N)")
if continue_game.lower() == 'n':
break
def instructions(p1,p2):
print(p1,p2,"I will select a secret word and give you the number of the word.")
print("You need to guess the letters in the word.")
print("Correctly guessed letters will be uesd to fill in the blank, incorrecly guessed letters will be used to build a snowman.")
print("The game will end if you successfully guess all the secret letters or you have 5 wrong guesses.")
def getGuess(wrongList,player):
letter = input(player + " Guess a letter: ")
punct = "~!@#$%^&*()_+?:"
if letter in wrongList:
print("You have guessed this letter.")
return getGuess(wrongList, player)
if letter in punct:
print("This guess is invaild, guess again!")
return getGuess(wrongList, player)
else:
letter = letter.lower()
return letter
def statusReport(wrongList,wrongCount,blanks):
print("Current status: ")
print("You have guessed",wrongCount,"times wrong.")
print("The letters you have guessed wrong are",wrongList)
print("The secret word now is",blanks)
def initializeBlanks(s):
blanks = "#" * len(s)
return blanks
def fillInBlanks(secretWord,blanks,l,player):
locations =
for location in locations:
blanks = blanks[:location] + l + blanks
print(blanks)
if not "#" in blanks:
print(player,"Congratulations! You are the winner!")
return blanks
def display(wrongCount):
grid = [
"_____",
"| |",
"|( )",
"|/|\\",
"|/ \\",
"|______"
]
Dict = {
1: grid,
2: "\n".join(grid[:2]),
3: "\n".join(grid[:3]),
4: "\n".join(grid[:4]),
5: "\n".join(grid)
}
for key in (Dict.keys()):
if key == wrongCount:
print(Dict)
main()
以上回复来自 -- ChatGPT(FishC官方接口),如未能正确解答您的问题,请继续追问。
页:
[1]