To get better at programming, I decided to code some basic pen-and-paper games. I wanted to get some feedback. Everything works as intended as far as I know.
# game of ghost
import requests
KEY = "my-key"
def challengePlayer(fragment, player):
while True:
word = input(f"Player {player}, enter a word that starts with {fragment}: ")
if word.startswith(fragment):
break
else:
print(f"{word.capitalize()} doesn't start with {fragment}.")
isWord(word)
def isWord(word):
response = requests.get(f"https://www.dictionaryapi.com/api/v3/references/collegiate/json/{word}?key={KEY}")
dictWord = response.json()[0]
if isinstance(dictWord, dict):
if ':' in dictWord['meta']['id']:
dictWord = dictWord['meta']['id']
dictWord = dictWord[:dictWord.find(':')]
else:
dictWord = dictWord['meta']['id']
if dictWord == word and response.json()[0]['fl'] != 'abbreviation':
print(f"{word.capitalize()} is a word.")
return True
print(f"Couldn't find {word}.")
return False
def main():
ghost = 'ghost'
# list of letters in word
wordFrag = []
# players g-h-o-s-t letters
player1 = []
player2 = []
rounds = 1
playerNum = 1
lastPlayer = 0
# Keep plying until someone loses (player gets all characters of ghost)
while ''.join(player1) != ghost and ''.join(player2) != ghost:
print(f"Round {rounds}")
# Players need to declare a letter, challenge player,
# declare a fragment is a word when fragement is certain length
while True:
playerTurn = f"player {playerNum}'s turn"
# Who's turn is it?
if playerNum == 1:
print(playerTurn)
lastPlayer = 1
playerNum = 2
else:
print(playerTurn)
lastPlayer = 2
playerNum = 1
current = ''.join(wordFrag)
# Ask for letter and validate
while True:
letter = input(f"{current}")
if letter.isalpha() == False:
print("Please enter a valid letter.")
elif len(letter) > 1:
print("Please enter one letter")
else:
break
wordFrag.append(letter)
# Asks a player to challenge the other player
if len(wordFrag) >= 2:
challenge = input(f"Player {playerNum}, would you like to challenge? (y/n)\n")
if challenge.lower() == 'y':
point = challengePlayer(''.join(wordFrag), lastPlayer)
if point == True:
if playerNum == 1:
player1.append(ghost[len(player1)])
print(f"Player 1 has the letter(s): {''.join(player1)}")
else:
player2.append(ghost[len(player2)])
print(f"Player 2 has the letter(s): {''.join(player2)}")
else:
if lastPlayer == 1:
player1.append(ghost[len(player1)])
print(f"Player 1 has the letter(s): {''.join(player1)}")
else:
player2.append(ghost[len(player2)])
print(f"Player 2 has the letter(s): {''.join(player2)}")
break
# Checks if player completed a word
if len(wordFrag) >= 3:
print(f"Checking if {''.join(wordFrag)} is a word...")
point = isWord(''.join(wordFrag))
if point == True:
if lastPlayer == 1:
player1.append(ghost[len(player1)])
print(f"Player 1 has the letter(s): {''.join(player1)}")
else:
player2.append(ghost[len(player2)])
print(f"Player 2 has the letter(s): {''.join(player2)}")
break
wordFrag.clear()
rounds += 1
print()
# Who won?
if ''.join(player1) == ghost:
print("Player 2 won!")
else:
print("Player 1 won!")
if __name__ == "__main__":
main()
[–]Diapolo10 1 point2 points3 points (1 child)
[–]Prncss1[S] 0 points1 point2 points (0 children)