Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Name: Kieran Tait
- # Student Number:
- # This file is provided to you as a starting point for the "wordchain.py" program of Assignment 2
- # of CSP1150/CSP5110 in Semester 1, 2018. It aims to give you just enough code to help ensure
- # that your program is well structured. Please use this file as the basis for your assignment work.
- # You are not required to reference it.
- # The "pass" command tells Python to do nothing. It is simply a placeholder to ensure that the starter files run smoothly.
- # They are not needed in your completed program. Replace them with your own code as you complete the assignment.
- # Import the necessary modules.
- import random
- import urllib.request
- import json
- import string
- #function for player names
- def inputPlayerName(numPlayers):
- #iteration on all players
- for x in range(1,numPlayers+1):
- print ("\nPlayer ",x)
- while True:
- playername=str(input("What is your name? "))
- #check if input is only letters
- if not playername.isalpha():
- print("Your name must consist of letters")
- else:
- print("Thank you.")
- #add name to list of names
- playerNames.append(playername)
- break
- #function for word inputs
- def inputWord(word):
- usedWords = []
- while True:
- if word in usedWords:
- print("This word has already been used!")
- print("Chain has ended!")
- chain = 0
- gamestatus = False
- #check if input is only letters
- if not word.isalpha():
- print("Please enter an answer consisting of letters")
- if word[0] not in letters[-1]:
- print("The word you entered does not begin with "+letters[-1]+"!")
- print("Chain has ended!")
- chain = 0
- gamestatus = False
- break
- else:
- print("Correct!")
- #add word to list of used words
- usedWords.append(word)
- letter = word[-1]
- letters.append(letter)
- chain = chain +1
- gamestatus = True
- break
- # Initialise variables (Requirement 1).
- chain = 0
- wordTypes = ["Noun", "Verb", "Adjective"]
- playerNames = []
- usedWords = []
- letters = []
- # Get player count and names (Requirements 2-3).
- print("Hello! Welcome to Word Chain!")
- count = 0
- while True:
- numPlayers = int(input("\nHow many players? (2 Minimum): "))
- if numPlayers < 2:
- print('Please enter a minimum of 2 players.')
- continue
- else:
- break
- for player in range(numPlayers):
- #put inputWord in here after append. playerNames.append(inputWord("Enter..))
- inputPlayerName(numPlayers)
- if len(playerNames) == numPlayers:
- break
- # Begin main gameplay loop (Requirement 4).
- if chain == 0:
- letter = random.choice(string.ascii_letters)
- letters.append(letter)
- word = input("\nEnter a "+str(random.choice(wordTypes))+" beginning with "+str(letters[-1])+"!: ")
- inputWord(word)
- usedWords.append(word)
- if gamestatus = True:
- word = word = input("Enter a "+str(random.choice(wordTypes))+" beginning with "+str(letters[-1])+"!: ")
- inputWord(word)
- usedWords.append(word)
- # Show final chain length and record a log of the game (Requirement 5).
- #with open("logs.txt", "a") as file:
- #json.dump({'players':players, 'names':playerNames, 'chain':chain}, file, indent=4)
- #file.close()
- else:
- print("player names list: ",playerNames)
- print("used word list: ",usedWords)
- print("chain value: ",chain)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement