I am trying to get this code on Python 3.6.3 to be:
More efficient
More readable
More simple
This code is supposed to be like hangman, just without the stickman. I also have added code which will count the amount of times you have won, lost and how many times you have played the game.
(I might put a bit of SQLite into this code so it stores how many points the player has, etc.)
However, the code below is extremely long, and I think that it could be greatly shortened.
Here is my code:
from random import randint as r
game_times = 0
if game_times == 0:
lives = 10
game = True
game_wins = 0
game_losses = 0
points = 0
space_1 = '__'
space_2 = '__'
space_3 = '__'
space_4 = '__'
space_5 = '__'
word_chooser_easy = (r(0, 5))
while game:
#this chooses a random word.
if word_chooser_easy == 0:
hangman_word = 'later'
letter_one = 'l'
letter_two = 'a'
letter_three = 't'
letter_four = 'e'
letter_five = 'r'
elif word_chooser_easy == 1:
hangman_word = 'point'
letter_one = 'p'
letter_two = 'o'
letter_three = 'i'
letter_four = 'n'
letter_five = 't'
elif word_chooser_easy == 2:
hangman_word = 'water'
letter_one = 'w'
letter_two = 'a'
letter_three = 't'
letter_four = 'e'
letter_five = 'r'
elif word_chooser_easy == 3:
hangman_word = 'joker'
letter_one = 'j'
letter_two = 'o'
letter_three = 'k'
letter_four = 'e'
letter_five = 'r'
elif word_chooser_easy == 4:
hangman_word = 'maths'
letter_one = 'm'
letter_two = 'a'
letter_three = 't'
letter_four = 'h'
letter_five = 's'
elif word_chooser_easy == 5:
hangman_word = 'crazy'
letter_one = 'c'
letter_two = 'r'
letter_three = 'a'
letter_four = 'z'
letter_five = 'y'
#now we need to check if the person has won the game.
if space_1 != "__" and space_2 != "__" and space_3 != "__" and space_4 != "__" and space_5 != "__":
#print your congratulations for winning
print("Well done! You have won the game!")
game_times += 1
game_wins += 1
game = False
continue
#print the hangman.
print("This is your hangman:")
print(space_1 , " " , space_2 , " " , space_3 , " " , space_4 , " " , space_5)
which_letter = input("Which letter do you want to choose?")
if letter_one == which_letter:
#this means the player has guessed space 1 correctly.
print("Well done! You have guessed it correctly!")
space_1 = letter_one
elif letter_two == which_letter:
#this means the player has guessed space 2 correctly.
print("Well done! You have guessed it correctly!")
space_2 = letter_two
elif letter_three == which_letter:
#this means the player has guessed space 3 correctly.
print("Well done! You have guessed it correctly!")
space_3 = letter_three
elif letter_four == which_letter:
#this means the player has guessed space 4 correctly.
print("Well done! You have guessed it correctly!")
space_4 = letter_four
elif letter_five == which_letter:
#this means the player has guessed space 5 correctly.
print("Well done! You have guessed it correctly!")
space_5 = letter_five
else:
#this means the player got it wrong.
print("You were wrong...")
lives -= 1
if lives == 0:
print("Game over...")
game_losses += 1
game_times += 1
game = False
continue
else:
print("You now have" , lives , "lives left...")
else:
stats = input("Do you want to see your stats so far? y/n")
if stats == "y":
print("You have lost" , game_losses , "times, you have won" , game_wins , "times and you have played hangman" , game_times , "times.")
else:
pass
print("Your second game will be one that has more double lettered words in it...")
lives = 8
word_chooser_easy = (r(0, 5))
space_1 = '__'
space_2 = '__'
space_3 = '__'
space_4 = '__'
space_5 = '__'
while game:
#this chooses a random word.
if word_chooser_easy == 0:
hangman_word = 'later'
letter_one = 'l'
letter_two = 'a'
letter_three = 't'
letter_four = 'e'
letter_five = 'r'
elif word_chooser_easy == 1:
hangman_word = 'point'
letter_one = 'p'
letter_two = 'o'
letter_three = 'i'
letter_four = 'n'
letter_five = 't'
elif word_chooser_easy == 2:
hangman_word = 'water'
letter_one = 'w'
letter_two = 'a'
letter_three = 't'
letter_four = 'e'
letter_five = 'r'
elif word_chooser_easy == 3:
hangman_word = 'joker'
letter_one = 'j'
letter_two = 'o'
letter_three = 'k'
letter_four = 'e'
letter_five = 'r'
elif word_chooser_easy == 4:
hangman_word = 'maths'
letter_one = 'm'
letter_two = 'a'
letter_three = 't'
letter_four = 'h'
letter_five = 's'
elif word_chooser_easy == 5:
hangman_word = 'crazy'
letter_one = 'c'
letter_two = 'r'
letter_three = 'a'
letter_four = 'z'
letter_five = 'y'
#now we need to check if the person has won the game.
if space_1 != "__" and space_2 != "__" and space_3 != "__" and space_4 != "__" and space_5 != "__":
#print your congratulations for winning
print("Well done! You have won the game!")
game_times += 1
game_wins += 1
game = False
continue
#print the hangman.
print("This is your hangman:")
print(space_1 , " " , space_2 , " " , space_3 , " " , space_4 , " " , space_5)
which_letter = input("Which letter do you want to choose?")
if letter_one == which_letter:
#this means the player has guessed space 1 correctly.
print("Well done! You have guessed it correctly!")
space_1 = letter_one
elif letter_two == which_letter:
#this means the player has guessed space 2 correctly.
print("Well done! You have guessed it correctly!")
space_2 = letter_two
elif letter_three == which_letter:
#this means the player has guessed space 3 correctly.
print("Well done! You have guessed it correctly!")
space_3 = letter_three
elif letter_four == which_letter:
#this means the player has guessed space 4 correctly.
print("Well done! You have guessed it correctly!")
space_4 = letter_four
elif letter_five == which_letter:
#this means the player has guessed space 5 correctly.
print("Well done! You have guessed it correctly!")
space_5 = letter_five
else:
#this means the player got it wrong.
print("You were wrong...")
lives -= 1
if lives == 0:
print("Game over...")
game_losses += 1
game_times += 1
game = False
continue
else:
print("You now have" , lives , "lives left...")