|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
这是我写完成的代码,请问怎么做到让一个玩家赢了之后可以一直让他猜?等他猜错之后就换另一个人猜?
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)
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):
location=secretWord.index(l)
blanks[location]=l
print(blanks)
if not "#" in blanks:
print(player,"Congratulations! You are the winner!")
return blanks
def display(wrongCount):
grid=[[' ',' ',' '],
[' ',' ',' '],
[' ',' ',' '],
['~','~','~']]
Dict={1:[0,1,'O'],
2:[1,1,'O'],
3:[2,1,'O'],
4:[1,0,'/'],
5:[1,2,'\\']}
for key in (Dict.keys()):
if key<=wrongCount:
value=Dict[key]
row=value[0]
col=value[1]
item=value[2]
grid[row][col]=item
for row in grid:
for col in row:
print(col,end=" ")
print("\n")
main()
|
|