我很难弄清楚如何让这个工作。基本上我必须制作一个游戏,其中一个单词是从words.txt文件中随机生成的,然后交替的玩家必须输入一个以前一个单词的最后一个字母开头的单词。我想我已经掌握了它的基本要点,但是我需要解决一些问题。
首先,有人可以告诉我为什么我的代码不起作用?当我运行它并键入一个应该完成的单词== True时,它似乎无法识别它。 代码如下:
import random
def dictionary_list():
dictionary = open("words.txt","r")
dictionary_list = dictionary.read()
dictionary_list = dictionary_list.split()
dictionary.close()
return dictionary_list
def start_word(dictionary_list):
random_number = random.randrange(0,len(dictionary_list))
random_word = dictionary_list[random_number]
print("Starting word: ", random_word)
return random_word
def get_word(player_number):
word = input(player_number)
return word
def get_player_number(player_number):
if player_number == 1:
player_number = 2
else:
player_number = 1
return player_number
def main():
word_list = dictionary_list()
starting_word = start_word(word_list)
words_used_list = [starting_word]
print(words_used_list)
finished = False
previous_word = starting_word
count = 0
player_number = 1
while finished == False:
word = get_word(player_number)
last_letter_word = previous_word[-1]
if word[0] != last_letter_word:
finished == True
elif word not in word_list:
finished == True
elif word in words_used_list:
finished == True
elif finished == True:
print("=" * 40)
print("Winner is player",player_number)
print("Number of words played", count)
print("List of words:", words_used_list)
print("Losing word: ", word)
print("=" * 40)
else:
player_number = get_player_number(player_number)
previous_word = word
count = count + 1
words_used_list.append(word)
main()
以下是所需输出的截图: http://imgur.com/S9a2ema
答案 0 :(得分:1)
问题是你有这个
elif word in words_used_list:
finished == True
你的意思是:
elif word in words_used_list:
finished = True
(和其他两个地方类似)
另请注意,您可以说
while finished:
而不是
while finished == True:
可能被认为更优雅。