I have recently been diving into Python. I have been making really small programs like this one and I am not confident in how practical I am writing Python. This is because I am not familiar with the indent block coding.
Do I have any redundancies? Is there too much stuff going on in the game function?
import random
def getRandom():
# will get a random word out of list of words
answerList = ['toy', 'plane', 'bannana', 'wedding',
'computer']
random.shuffle(answerList)
answer = list(answerList[0])
return answer
def parseGuess(guess):
guess = list(guess)
if len(guess) == 1:
return True
else:
return False
def playAgain(stats):
print(stats["Tries"])
print("\n It took you {} tries to guess {}".format(stats["Tries"], stats["Words"][0]))
user = input("Would you like to play again Y/n: ").lower()
if user != 'y':
print("Thanks for playing. Bye")
else:
game()
def game():
correctAnswerCount = 0
answer = getRandom()
usedLetters = []
display = []
display.extend(answer)
stats = {
"Words": [],
"Tries": 0
}
# CONVERT EVERY ELEMENT IN THE DISPLAY LIST TO AN UNDERSOCORE
for i in range(len(answer)):
display[i] = "_"
print("========== \n{}\n==========".format(" ".join(display)))
print("\nCorrect answerCount: {}/{}".format(correctAnswerCount, len(answer)))
while correctAnswerCount < len(answer):
guess = input("Please only choose a letter: ").lower()
stats["Tries"] += 1
# CHECK TO SEE IF THE USER ONLY ENTERED ONE LETTER
if parseGuess(guess):
usedLetters.append(guess)
for i in range(len(answer)):
if guess == answer[i]:
display[i] = guess
correctAnswerCount += 1
else:
guess = input("Please only choose ONE a letter: ").lower()
print("========== \n{}\n==========".format(" ".join(display)))
print("\nCorrect answerCount: {}/{} \n ".format(correctAnswerCount, len(answer)))
print()
print(" letters used: \n{}".format(" ".join(usedLetters)))
stats["Words"].append("".join(answer))
print("Well done you guessed the corrext word")
playAgain(stats)
game()