当我为choose_word创建word_blank_list(用户的空白列表(时,当我试图将其从列表中删除时,该单词会被更改且无法识别,因为它是一个新词。代码和错误消息如下。代码的其余部分工作正常。这是最后一步。非常感谢。
#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 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
非常感谢!
更简单的代码不太容易出错:
- 从url中读取所有单词一次,填充列表,随机洗牌
- 将第一个单词作为混洗列表中的当前单词一次
- 将单词设为小写,并创建一个长度相同的列表,每个字母带有"*">
- [实际游戏]进行猜测,如果失败,则进行计数,如果完成,则在每轮结束时进行检查:
- 当尝试===7或猜中单词时完成,然后进行6,否则进行4
- 有条件地跳到2。新一轮
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