Question: Hi I need assistance with my Python code. Which was working to some degree and then stopped. I am creating a wordchain using Wordnik, but

Hi

I need assistance with my Python code. Which was working to some degree and then stopped. I am creating a wordchain using Wordnik, but for some reason I am now getting errors on after entering 2 players and then proceed to enter the first player and then the second player. Its when I enter the first player name it should loop to the next name but it throws a traceback error. For the life of me I can't figure out what I've done wrong. I have attached my code. Any assistance would be greatly appreciated. Thanks.

# Import the necessary modules. import random import json import sys import urllib

# start def main(): # Initialise variables (Requirement 1). chain = 0 wordTypes = ("noun", "verb", "adjective") playerNames = [] usedWords = []

print('Welcome to the WordChain game. We hope you enjoy!')

while True: try: numPlayers = int(input(" Enter the number of Players: ")) if numPlayers < 2: raise ValueError break except ValueError: print("Number of Players must be integer above 2.")

# getting names for player in range(numPlayers): playerNames.append(inputWord("Enter name of player "+str(player + 1)+" :"))

turn = 0 # main game loop while True: wordType = random.choice(wordTypes)

firstLetter = random.choice('abcdefghijklmnopqrstuvwxyz') if chain == 0 else usedWords[chain - 1][-1]

print(playerNames[turn], 'is up next') word = inputWord('Enter a ' + wordType + ' beginning with "'+firstLetter+'" !').lower()

if word[0] is not firstLetter: print("Word chain broken -", word, 'does not start with"'+firstLetter+'" !') break # chain broken

if word in usedWords: print("Word chain broken -", word, 'is already used before') break # chain broken

# fetch info from api response = urllib.request.urlopen('http://api.wordnik.com:80/v4/word.json/' + word + '/definitions?limit=5&includeRelated=true&useCanonical=false&includeTags=false&api_key=6f4a1d7e93de5d79f4005055d18056585ba655297b9c7d569') data = response.read() encoding = response.info().get_content_charset('utf-8') def_list = json.loads(data.decode(encoding)) if len(def_list) == 0: print("Word chain broken -", word, 'is not recognised') break # chain broken

found = -1 for index in range(len(def_list)): if def_list[index]['partOfSpeech'] == wordType: found = index break

if found == -1: print("Word chain broken -", word, 'is not a', wordType) break # chain broken

# valid word chain += 1 usedWords.append(word) print('Good job,', str(playerNames[turn]), '- "' + word + '" is an', wordType, "defined as... " + def_list[found]['text']) print('the word chain is now', chain, 'links long') turn = turn + 1 if turn + 1 is not numPlayers else 0

print("final chain length:", chain)

saveDict = {"players": numPlayers, "names": playerNames, "chain": chain}

log_json = []

try: with open('log.txt') as file: log_json = json.loads(file.read()) except FileNotFoundError: with open('log.txt', 'w'): pass

log_json.append(saveDict)

with open('log.txt', 'w') as file: file.write(json.dumps(log_json))

print('log saved')

def inputWord(promp): while True: try: toReturn = input(promp)

if not toReturn.isalpha(): raise ValueError break except ValueError: print("Please enter only alpha letter characters word least one character long")

return toReturn

if __name__ == "__main__": main() # This function repeatedly prompts for input until the user enters # something at least one character long and entirely alphabetic. # See the "The 'inputWord' Function" section of the assignment brief

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!