Python 刽子手在整个游戏中改变了游戏

Python 刽子手在整个游戏中改变了游戏,python,function,loops,Python,Function,Loops,当我为choose_word创建单词_blank_列表(用户的空白列表)时,该单词已更改且无法识别,因为当我尝试将其从列表中删除时,它是一个新词。下面是代码和错误消息。代码的其余部分工作正常。这是最后一步。谢谢大家! #Choose a word import urllib.request import random def choose_word(): word_file = 'https://raw.githubusercontent.com/lizzierobinson2/coolc

当我为choose_word创建单词_blank_列表(用户的空白列表)时,该单词已更改且无法识别,因为当我尝试将其从列表中删除时,它是一个新词。下面是代码和错误消息。代码的其余部分工作正常。这是最后一步。谢谢大家!

#Choose a word
import urllib.request
import random

def choose_word():
  word_file = 'https://raw.githubusercontent.com/lizzierobinson2/coolcode/master/list%20of%20words'
  my_file = urllib.request.urlopen(word_file)
  chosen_word = my_file.read()
  chosen_word = chosen_word.decode("utf-8")
  list_of_words = chosen_word.split("\n")
  for x in range(1):
    return random.choice(list_of_words)
    break
  
print('Welcome to the game hangman! The blanks of a word I have chosen are printed below. :)')

  #Create a list that holds the word and makes each letter a string
word_blank_list = [choose_word()]
length = len(choose_word())

for spaces in range(length):
  word_blank_list.append('_')

#word_blank_list.remove(choose_word())
print(word_blank_list)

word_list = [choose_word()]
string = ''
string = string.join(word_list)

word_string = list(string)

  #Define print_body and say what happens when the letter is not in the word
def print_body(number):
  if number == 1:
    print("  \|/  ")
  if number == 2:
    print("  \|/  ")
    print("  (_)  ")
  if number == 3:
    print("  \|/  ")
    print("  (_)  ")
    print("   |   ")
    print("   |   ")
  if number == 4:
    print("  \|/  ")
    print("  (_)  ")
    print("  /|   ")
    print(" / |   ")
  if number == 5:
    print("  \|/  ")
    print("  (_)  ")
    print("  /|\  ")
    print(" / | \ ")
  if number == 6:
    print("  \|/  ")
    print("  (_)  ")
    print("  /|\  ")
    print(" / | \ ")
    print("  /    ")
    print(" /     ")
  if number == 7:
    print("  \|/  ")
    print("  (_)  ")
    print("  /|\  ")
    print(" / | \ ")
    print("  / \  ")
    print(" /   \ ")

num_of_tries = 0

while True:
  ask_guess = None
  #Ask the user for a letter
  letter_ask = input('Enter a letter. ').lower()
  #printing 2 of same letter
  placement = []
  guess = letter_ask
  for x in range(len(word_string)):
    if word_string[x] == guess:
      placement.append(x)
  for x in placement:
    word_blank_list[x] = guess    

  if letter_ask not in word_string:
    num_of_tries = num_of_tries + 1
    print('This letter is not in the word. A part of the body has been drawn. Try again.')
    print_body(num_of_tries)
       
    #What happens when the letter is in the word
  if letter_ask in word_string:
    location = word_string.index(letter_ask)
    word_blank_list[location] = letter_ask
    print(word_blank_list)
    ask_guess = input('Would you like to guess what the word is? ').lower()
    
    if ask_guess == 'yes':
      guess = input('Enter your guess. ').lower()   
         
    if ask_guess == 'no':
      continue

    #How to stop the game
  if num_of_tries == 7:
    print('You lose :(')
    break
  if guess == choose_word():
    print('Congrats! You win!')
    break 
  if guess != choose_word() and ask_guess == 'yes':
    print('This is incorrect.')
    continue 
ValueError回溯(最近一次调用)
在()
22字空白列表。附加(“”)
23
--->24字空白列表。删除(选择单词()
25打印(word\u空白\u列表)
26
ValueError:list。删除(x):x不在列表中

非常感谢你

更简单的代码更不容易出错:

  • 阅读url中的所有单词一次,填充一个列表,随机洗牌
  • 从无序列表中将第一个单词作为当前单词
  • 将单词全部小写,并创建一个长度相同的列表,每个字母带有“*”
  • [实际游戏]询问猜测,如果错误,则计算尝试次数,如果完成,则在每轮结束时检查:
  • 当尝试==7或猜到单词时完成,然后执行6,否则执行4
  • 有条件地跳到2。新一轮


  • 为什么对范围(1)中的x使用
    :返回random.choice(单词列表);break
    -just
    return random.choice(单词列表)
    我添加了这个选项来解决这个问题,这样单词只能被选择一次,但它不起作用。如果我删除它,它仍然不起作用。
    choose\u word()
    返回一个随机单词,
    word\u blank\u list=[choose\u word()]
    将在列表中存储该随机单词,但
    length=len(choose\u word())
    将获得一个新随机单词的长度和
    word\u blank\u列表。删除(choose\u word())
    将尝试删除不在列表中的新随机单词。选择一次单词并将其存储
    word=choose_word()
    然后参考该单词查看您的资料,如
    len(word)
    谢谢!这非常有效。
    ValueError                                Traceback (most recent call last)
    <ipython-input-42-0f2af26177a0> in <module>()
         22   word_blank_list.append('_')
         23 
    ---> 24 word_blank_list.remove(choose_word())
         25 print(word_blank_list)
         26 
    
    ValueError: list.remove(x): x not in list
    
    import urllib.request
    import random
    
    def get_all_words():
        word_file = 'https://raw.githubusercontent.com/lizzierobinson2/coolcode/master/list%20of%20words'
        return urllib.request.urlopen(word_file).read().decode("utf-8").split("\n")
    
    list_of_words = get_all_words()
    random.shuffle(list_of_words)
    
    # loop until the user does not want to continue or the words are all guessed at
    while True: 
        # prepare stuff for this round - the code for the rounds of guessing never
        # touches the "word" setup - only ui and tries get modified
        word_to_guess = list_of_words.pop()
        lower_word = word_to_guess.lower()
        ui = ['*']*len(word_to_guess)
        tries = 0
    
        # gaming round with 7 tries
        while True:
            print("The word: " + ''.join(ui))
            letter = input("Pick a letter, more then 1 letter gets ignored: ").lower().strip()[0]
    
            # we just compare the lower case variant to make it easier
            if letter not in lower_word:
                tries += 1
                # print your hangman here
                print(f"Errors: {tries}/7")
            else:
                # go over the lower-case-word, if the letters match, place the letter
                # of the original word in that spot in your ui - list 
                # string.index() only reports the FIRST occurence of a letter, using 
                # this loop (or a list comp) gets all of them
                for i,l in enumerate(lower_word):
                    if l == letter:
                        ui[i] = word_to_guess[i]
    
            # test Done-ness:
            if word_to_guess == ''.join(ui):
                print("You won.")
                break
            elif tries == 7:
                print("You lost.")
                break
        # reset tries
        tries = 0
        if not list_of_words:
            print("No more words to guess. Take a break.")
            break
        elif input("Again? [y/ ]").lower() != 'y':
            break