Advertisement
Guest User

Kierans

a guest
May 27th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.68 KB | None | 0 0
  1. # Name: Kieran Tait
  2. # Student Number:
  3.  
  4. # This file is provided to you as a starting point for the "wordchain.py" program of Assignment 2
  5. # of CSP1150/CSP5110 in Semester 1, 2018. It aims to give you just enough code to help ensure
  6. # that your program is well structured. Please use this file as the basis for your assignment work.
  7. # You are not required to reference it.
  8.  
  9. # The "pass" command tells Python to do nothing. It is simply a placeholder to ensure that the starter files run smoothly.
  10. # They are not needed in your completed program. Replace them with your own code as you complete the assignment.
  11.  
  12.  
  13. # Import the necessary modules.
  14. import random
  15. import urllib.request
  16. import json
  17. import string
  18.  
  19. #function for player names
  20. def inputPlayerName(numPlayers):
  21. #iteration on all players
  22. for x in range(1,numPlayers+1):
  23. print ("\nPlayer ",x)
  24.  
  25. while True:
  26. playername=str(input("What is your name? "))
  27.  
  28. #check if input is only letters
  29. if not playername.isalpha():
  30. print("Your name must consist of letters")
  31. else:
  32. print("Thank you.")
  33. #add name to list of names
  34. playerNames.append(playername)
  35. break
  36.  
  37. #function for word inputs
  38. def inputWord(word):
  39. usedWords = []
  40. while True:
  41. if word in usedWords:
  42. print("This word has already been used!")
  43. print("Chain has ended!")
  44. chain = 0
  45. gamestatus = False
  46.  
  47. #check if input is only letters
  48. if not word.isalpha():
  49. print("Please enter an answer consisting of letters")
  50.  
  51. if word[0] not in letters[-1]:
  52. print("The word you entered does not begin with "+letters[-1]+"!")
  53. print("Chain has ended!")
  54. chain = 0
  55. gamestatus = False
  56. break
  57.  
  58. else:
  59. print("Correct!")
  60. #add word to list of used words
  61. usedWords.append(word)
  62. letter = word[-1]
  63. letters.append(letter)
  64. chain = chain +1
  65. gamestatus = True
  66. break
  67.  
  68. # Initialise variables (Requirement 1).
  69. chain = 0
  70. wordTypes = ["Noun", "Verb", "Adjective"]
  71. playerNames = []
  72. usedWords = []
  73. letters = []
  74. # Get player count and names (Requirements 2-3).
  75. print("Hello! Welcome to Word Chain!")
  76.  
  77. count = 0
  78.  
  79. while True:
  80. numPlayers = int(input("\nHow many players? (2 Minimum): "))
  81. if numPlayers < 2:
  82. print('Please enter a minimum of 2 players.')
  83. continue
  84. else:
  85. break
  86.  
  87. for player in range(numPlayers):
  88. #put inputWord in here after append. playerNames.append(inputWord("Enter..))
  89. inputPlayerName(numPlayers)
  90. if len(playerNames) == numPlayers:
  91. break
  92.  
  93. # Begin main gameplay loop (Requirement 4).
  94.  
  95. if chain == 0:
  96. letter = random.choice(string.ascii_letters)
  97. letters.append(letter)
  98. word = input("\nEnter a "+str(random.choice(wordTypes))+" beginning with "+str(letters[-1])+"!: ")
  99. inputWord(word)
  100. usedWords.append(word)
  101.  
  102. if gamestatus = True:
  103. word = word = input("Enter a "+str(random.choice(wordTypes))+" beginning with "+str(letters[-1])+"!: ")
  104. inputWord(word)
  105. usedWords.append(word)
  106.  
  107. # Show final chain length and record a log of the game (Requirement 5).
  108. #with open("logs.txt", "a") as file:
  109. #json.dump({'players':players, 'names':playerNames, 'chain':chain}, file, indent=4)
  110. #file.close()
  111. else:
  112. print("player names list: ",playerNames)
  113. print("used word list: ",usedWords)
  114. print("chain value: ",chain)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement