THIS IS HOMEWORK 我試圖讓該程序,當用戶說,他們不想再玩了打破的,我不能讓我的劊子手計劃打破,但我無法讓它工作。該程序告訴用戶再次嘗試,但保持相同的單詞,不會中斷。我的循環有什麼問題嗎?如果不是,那麼問題是什麼,我該如何解決?當用戶用完猜測
在此先感謝
def playGame():
'''This function allows you to play hangman'''
print("Welcome to Hangman")
print(" -------")
print(" | |")
print(" |")
print(" |")
print(" |")
print(" -----")
#def randword(List):
#wr=random.randint(0,len(List)-1)
#return List[wr]
global wordlist
hangman=open('hangman.txt','r+')
wordlist=hangman.read()
x=random.randint(0,len(wordlist)-1)
w=random.choice(wordlist)
print (w)
blanks= '_ ' * len(w)
#for i in range(len(w)):
#print('_',sep=" ")
#print()
guesses=[]
correct=[]
mistakes=0
correctguess=0
guessNumber=0
while guessNumber<=6:
print(blanks)
letter=str(input("Take a guess?"))
if letter in guesses:
print("You tried that already! Enter another letter!")
continue
if len(letter) > 1:
print("""Enter ONE letter!""")
if letter not in guesses:
guesses.append(letter)
if letter in w:
print("You got one, keep going")
correct.append(letter)
print (correct)
for i in range(len(w)):
if w[i]in correct:
correctguess=correctguess+1
if len(correct) == len(w):
print("You got it! The word was",w,)
replay=str(input(""" Want to try again? Enter yes or no"""))
if replay=='yes' or replay=='YES' or replay=='Yes':
playGame()
if replay=='no' or replay=='NO' or replay=='n' or replay=='this game sucked':
print("Well, have a nice day then")
break
elif letter not in w:
mistakes=mistakes+1
if mistakes == 1:
print(" -------")
print(" | |")
print(" o |")
print(" |")
print(" |")
print(" -----")
if mistakes == 2:
print(" -------")
print(" | |")
print(" o |")
print(" | |")
print(" |")
print(" -----")
if mistakes == 3:
print(" -------")
print(" | |")
print(" o |")
print(" /| |")
print(" |")
print(" -----")
if mistakes == 4:
print(" -------")
print(" | |")
print(" o |")
print(" /|\ |")
print(" |")
print(" -----")
if mistakes == 5:
print(" -------")
print(" | |")
print(" o |")
print(" /|\ |")
print("/ |")
print(" -----")
if mistakes == 6:
print(" -------")
print(" | |")
print(" o |")
print(" /|\ |")
print("/\ |")
print(" -----")
print("You did not get the word. Try again!")
replay=str(input(""" Want to try again? Enter yes or no"""))
if replay=='yes' or replay=='YES' or replay=='Yes':
playGame()
if replay=='no' or replay=='NO' or replay=='n' or replay=='this game sucked':
print("Well, have a nice day then")
break
print(guesses)
guessNumber = guessNumber+1
堆棧溢出不會爲你做功課。無論誰給你作業無疑都會爲你提供更合適的資源。 –