如果秘密单词中的所有字母都在guessed_letters列表中,如何在子手中结束游戏

时间:2019-05-29 02:49:16

标签: python for-loop

我正在尝试通过制作“如果所有字母都在gussed_letters中然后我结束游戏中的秘密单词中的每个字母”的for循环结束游戏。

我试图制作一个正确的字母列表,看看我的秘密单词中的所有字母是否都在我的正确的字母列表中,然后结束游戏,但我似乎无法正常工作。

import random



words = ['apple',' python','parent'] #Make list of words
def randomword(words): #takes in list words
  return random.choice(words) #returns a random element back
chosenword = randomword(words) # make a variable equal to the function


#variables (tries you get and list to letters gussed. )
tries = 10
guess_letters = []

def dashshow(guess_letters): #takes in guess_letters
  for letter in chosenword: #checks each letter in chosenword
    if letter in guess_letters: #if letter in guess_letters print that letter
      print(letter)
    else: #or else print a dash
      print('-')



def playgame(tries):# Takes tries
  while  tries != 0: #While tries is not 0
    guess = str(input("Guess a letter of the word: ")).lower() #Ask for a guess
    guess_letters.append(guess) #add guess to guess_letters list
    if guess in chosenword: #if your guess in chosenword 
      print("You got a letter correct!") 
      tries -= 1
    elif guess not in chosenword: 
      print("That letter is not in the word")
      tries -= 1
    dashshow(guess_letters) # last call the dashshow function


randomword(words)
playgame(tries)

2 个答案:

答案 0 :(得分:0)

我使dashshow函数对剩余的破折号进行计数,并返回是否剩余的值。如果不是,则游戏结束并获胜。

import random



words = ['apple',' python','parent'] #Make list of words
def randomword(words): #takes in list words
  return random.choice(words) #returns a random element back
chosenword = randomword(words) # make a variable equal to the function


#variables (tries you get and list to letters gussed. )
tries = 10
guess_letters = []

def dashshow(guess_letters): #takes in guess_letters
  dashes = 0
  for letter in chosenword: #checks each letter in chosenword
    if letter in guess_letters: #if letter in guess_letters print that letter
      print(letter)
    else: #or else print a dash
      print('-')
      dashes = dashes+1
  return(dashes>0)    



def playgame(tries):# Takes tries
  keepPlaying = True
  while  (tries != 0) and keepPlaying: #While tries is not 0
    guess = str(input("Guess a letter of the word: ")).lower() #Ask for a guess
    guess_letters.append(guess) #add guess to guess_letters list
    if guess in chosenword: #if your guess in chosenword 
      print("You got a letter correct!") 
      tries -= 1
    elif guess not in chosenword: 
      print("That letter is not in the word")
      tries -= 1
    if not dashshow(guess_letters): # last call the dashshow function
      keepPlaying=False
      print("you win!")


randomword(words)
playgame(tries)

答案 1 :(得分:0)

您可以创建变量为秘密单词的列表字母,如果相似,则删除字母

尝试一下

python manage.py runserver