我的python hangman游戏遇到了一些麻烦。我认为我的机制大部分都没有下降(不是100%自信),但是当用户输入信件时,我在打印板时遇到了一些问题。它应该找到存储的索引,然后转到该索引并用猜测的字母替换它。目前它并没有取代这封信,当它在一封猜测的信件后重新打印出来时它就会不断增加更多的星星。例如,如果单词长度为6个字母,则打印出6个星。当你猜一封信时,它会重新打印12星级的电路板,依此类推。如何在字母中填写字母并用填写的那个字母打印出相同的字母时,如何取代字母。当游戏结束时,如果他们想要继续播放并循环整个游戏,如何提示他们进入游戏再次?代码如下:
import random
listOfWords = ("hangman", "chairs", "backpack", "bodywash", "clothing", "computer", "python", "program", "glasses", "sweatshirt", "sweatpants", "mattress", "friends", "clocks", "biology", "algebra", "suitcase", "knives", "ninjas", "shampoo")
randomNumber = random.randint(0, len(listOfWords) - 1)
guessWord = listOfWords[randomNumber]
theBoard = []
wrong = []
guessedLetter = int(0)
#Will create the board using a new list and replace them with stars
def makeBoard(word):
for x in word: #will go through the number of letters in the word that is chosen and for each letter will append a star into theBoard list
theBoard.append('*')
return ''.join(theBoard) #Will print the list without having the [] and the commas
def guessing(letter): #This is for guessing the letters
win = int(0) #Will be used to see if the user wins
count = int(0) #Will be used to replace the star with a letter
if letter.lower() in guessWord: # converts to lowercase and goes through the word
guessedLetter = guessWord.index(letter) #create a variable called guessedLetter that will take the index of the letter guessed by the user
while (count != guessedLetter): #while loop to look for the index of the guessed letter
count += 1
if (count == guessedLetter): # will go into the board and replace the desired index with the letter if it matches
theBoard[count] = letter
for x in theBoard: # loop to go through theBoard to see if any '*' are there. If none are found it will say you win
if (x != '*'):
win += 1
if (win == len(theBoard)):
print("You win!")
def main():
print(guessWord)
level = input("Enter a difficulty level: ")
print("The word is: " + makeBoard(guessWord))
if (level == '1'):
print("You have selected the easy difficulty.")
while (len(wrong) != 9):
userGuess = input("Enter in the letter you want to guess: ")
guessing(userGuess)
if userGuess not in guessWord:
wrong.append(userGuess)
print("You have " + str(len(wrong)) + " guesses. Guessed letters: " + str(wrong))
if (len(wrong) == 9):
level = input("You have lost. If you would like to play again then enter in the difficulty level, or 4 to exit")
if (level != 4):
randomNewNumber = random.randint(0, len(listOfWords) - 1)
guessNewWord = listOfWords[randomNewNumber]
if (level == 4):
sys.exit(0)
if (level == '2'):
print("You have selected the medium difficulty.")
print("The word is: " + makeBoard(guessWord))
while (len(wrong) != 7):
userGuess = input("Enter in the letter you want to guess: ")
guessing(userGuess)
if userGuess not in guessWord:
wrong.append(userGuess)
print("You have " + str(len(wrong)) + " guesses. Guessed letters: " + str(wrong))
if (level == '3'):
print("You have selected the hard difficulty.")
print("The word is: " + makeBoard(guessWord))
while (len(wrong) != 5):
userGuess = input("Enter in the letter you want to guess: ")
guessing(userGuess)
if userGuess not in guessWord:
wrong.append(userGuess)
print("You have " + str(len(wrong)) + " guesses. Guessed letters: " + str(wrong))
main()
编辑:我已经解决了使用正确的字母重新显示的问题,除了第一个。例如,如果单词是python,它就会出现* ython,即使你猜它没有显示。以下是替换代码:
def guessing(letter): #This is for guessing the letters
win = int(0) #Will be used to see if the user wins
count = int(0) #Will be used to replace the star with a letter
guessedLetter = guessWord.index(letter) #create a variable called guessedLetter that will take the index of the letter guessed by the user
if letter.lower() in guessWord: # converts to lowercase and goes through the word
while (count != guessedLetter): #while loop to look for the index of the guessed letter
count = count + 1
if (count == guessedLetter): # will go into the board and replace the desired index with the letter if it matches
theBoard[count] = letter
print("The word is: " + ''.join(theBoard))
for x in theBoard: # loop to go through theBoard to see if any '*' are there. If none are found it will say you win
if (x != '*'):
win += 1
if (win == len(theBoard)):
print("You win!")
答案 0 :(得分:1)
我建议你保存"进展"在一个字符串变量(板)中,每当有人猜到一个好答案时替换它。我会编写一个可能有用的代码并给你一个提示。
import random
#global vars when starting the game
listOfWords = ["example", "says", "python", "rocks"]
guessWord = random.choice(listOfWords)
board = [" * " for char in guessWord]
alreadySaid = ""
#ready to rock and roll in a single loop
class Hangman():
def Playing():
global guessWord, board, alreadySaid
whatplayersaid = input("Guess a letter: ")
if whatplayersaid in guessWord:
board = [char if char == whatplayersaid or char in alreadySaid else " * " for char in guessWord]
board = "".join(board)
print(board)
else:
print("Nope")
alreadySaid = alreadySaid + whatplayersaid
Hangman.Playing()
Hangman.Playing()
:D这就是你想要完成的吗? (抱歉我的英语不好)
EDITTED 您可以将其保存在新的.py文件中,运行它并尝试说猜测,我希望您可以根据您的代码进行调整。
EDITTED x2 现在有一个循环本身,看看是否正常工作
答案 1 :(得分:1)
修改并测试了Saelyth的例子 - 现在它正在起作用游戏:)
#!/usr/bin/python3
import random
class Hangman():
def Playing(self):
listOfWords = ["example", "says", "python", "rocks"]
again = True
while again:
guessWord = random.choice(listOfWords)
board = "*" * len(guessWord)
alreadySaid = set()
mistakes = 7
print(" ".join(board))
guessed = False
while not guessed and mistakes > 0:
whatplayersaid = input("Guess a letter: ")
if whatplayersaid in guessWord:
alreadySaid.add(whatplayersaid)
board = "".join([char if char in alreadySaid else "*" for char in guessWord])
if board == guessWord:
guessed = True
else:
mistakes -= 1
print("Nope.", mistakes, "mistakes left.")
print(" ".join(board))
again = (input("Again [y/n]: ").lower() == 'y')
#----------------------------------------------------------------------
Hangman().Playing()
答案 2 :(得分:0)
你不需要你包含的一些代码稍微改了一下
import random
def Playing():
listOfWords = ["example", "says", "python", "rocks","test", "hangman"]`
again = True #variable created
while again: #created a loop
guessWord = random.choice(listOfWords) #choses a randomly out of the words in the variable listofWords
board = "-" * len(guessWord) #prints how many letters theirn are in the randomly chosen word in - format
alreadySaid = set()
mistakes = 7 # the msitakes start at 7
print(" ".join(board)) # joining other letters/ replace - wirh the actual letteers
guessed = False
while not guessed and mistakes > 0:
guess = input("Guess a letter: ") #tells you to guess a letter and input it
if guess in guessWord: #if the leter you guessed is in the randomly selected word
alreadySaid.add(guess) #add it to whats already been said
board = "".join([char if char in alreadySaid else "-" for char in guessWord]) # join them
if board == guessWord:
guessed = True
else:
mistakes -= 1 # minuses 1 from the value of mistakes (7) if you get the guess wrong
print("Nope.", mistakes, "mistakes left.") # if what you guessed is wrong then print nope and how many mistakes are remaining
print(" ".join(board))
print('well done')
again = (input("Would you like to go again [y/n]: ").lower() == 'y') # asks if you want to go again -- changes it to lower cap
Playing() # displays Playing() whithout you typing in Playing() for it to appear
答案 3 :(得分:0)
我喜欢MPM的版本。我只是微调了打印内容,以告诉获胜者是否输了或赢了。在以前的版本中,它打印“做得很好”。他们是赢还是输。
import random
def Playing():
listOfWords = ["example", "says", "python", "rocks","test", "hangman"]
again = True #variable created
while again: #created a loop
guessWord = random.choice(listOfWords) #choses a randomly out of the words in the variable listofWords
board = "*" * len(guessWord) #prints how many letters theirn are in the randomly chosen word in - format
alreadySaid = set()
mistakes = 7 # the msitakes start at 7
print(" ".join(board)) # joining other letters/ replace - wirh the actual letteers
guessed = False
while not guessed and mistakes > 0:
guess = input("Guess a letter: ") #tells you to guess a letter and input it
if guess in guessWord: #if the leter you guessed is in the randomly selected word
alreadySaid.add(guess) #add it to whats already been said
board = "".join([char if char in alreadySaid else "*" for char in guessWord]) # join them
if board == guessWord:
guessed = True
else:
mistakes -= 1 # minuses 1 from the value of mistakes (7) if you get the guess wrong
print("Nope.", mistakes, "mistakes left.") # if what you guessed is wrong then print nope and how many mistakes are remaining
print(" ".join(board))
if mistakes == 0:
print("Sorry! Game over.")
else:
print('Nice! You win.')
again = (input("Would you like to go again [y/n]: ").lower() == 'y') # asks if you want to go again -- changes it to lower cap
Playing() # displays Playing() whithout you typing in Playing() for it to appear
答案 4 :(得分:0)
当找到字母是否在guessWord
中时,将行count = count + 1
移至while
循环的末尾,如下所示:
def guessing(letter): #This is for guessing the letters
win = int(0) #Will be used to see if the user wins
count = int(0) #Will be used to replace the star with a letter
guessedLetter = guessWord.index(letter) #create a variable called guessedLetter that will take the index of the letter guessed by the user
if letter.lower() in guessWord: # converts to lowercase and goes through the word
while (count != guessedLetter): #while loop to look for the index of the guessed letter
if (count == guessedLetter): # will go into the board and replace the desired index with the letter if it matches
theBoard[count] = letter
print("The word is: " + ''.join(theBoard))
##Just Here!
count = count + 1
for x in theBoard: # loop to go through theBoard to see if any '*' are there. If none are found it will say you win
if (x != '*'):
win += 1
if (win == len(theBoard)):
print("You win!")