Reputation: 23
I'm having a hard time figuring out how to get this working. Basically i Have to make a game in which a word is randomly generated from a words.txt file and then alternating players have to type a word which starts with the last letter of the previous word. I think I have the basic jist of it but there are a few issues in which I need to iron out.
First off could someone please tell me why my code isn't working? When I run it and type a word that should make finished == True it doesnt seem to recognise it. Code is as follows:
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()
Here is a screenshot of what the required output should be like: https://i.sstatic.net/TQICn.jpg
Upvotes: 2
Views: 599
Reputation: 14685
The problem is that you have this
elif word in words_used_list:
finished == True
When you mean this:
elif word in words_used_list:
finished = True
(and similar in 2 other places)
Note also that you can say
while finished:
instead of
while finished == True:
which might be considered a little more elegant.
Upvotes: 1