Created
October 30, 2020 14:14
-
-
Save asad234/29ddfbf71491d1afe26595f30089ead1 to your computer and use it in GitHub Desktop.
Hangman Game by Abdalla using python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
import sys | |
def findloc(alph, word): | |
i = 0 | |
k = 0 | |
while (i < len(word)): | |
if (alph == word[i]): | |
loc.append(i) | |
k += 1 | |
i += 1 | |
def display(alph): | |
i = 0 | |
temp = [] | |
if (loc != None): | |
while (i < len(loc)): | |
check[loc[i]] = alph | |
i += 1 | |
i = 0 | |
while (i < len(word)): | |
if check[i] != None: | |
sys.stdout.write(check[i]) | |
temp.append(check[i]) | |
else: | |
sys.stdout.write(" _ ") | |
temp.append("_") | |
i += 1 | |
return "".join(temp) | |
# an array of movie names from where one movie name is picked randomly | |
lib = ["casablanca", "day and knight", | |
"jupiter", "interstellar", "gravity", "moonlight", | |
"inception", "waterhorse", "devil wears prada", | |
"bedazzled", "enchanted", "cinerella"] | |
loc = [] | |
hm = "hangman" | |
fin = "" | |
# picking random movie name | |
word = lib[random.randrange(len(lib))] | |
# getting its word count | |
check = [None] * len(word) | |
# initializing chances | |
chances = 0 | |
print("Guess a hollywood movie with ", len(word), " letters!!!") | |
print("") | |
print("let's begin"); | |
while (chances < 7): | |
print("") | |
# printing remaining chances by removing letters from word hangman when an entered character is not in movie name to be guessed | |
print(hm[chances:]); | |
lett = input("enter character--->") | |
# finding locations of entered letter | |
findloc(lett, word) | |
# displaying partially completed movie name after filling the entered letter | |
fin = display(lett) | |
# incrementing chances availed when a enterted letter is not part of moviename | |
if len(loc) == 0: | |
chances += 1 | |
loc = [] | |
# comparing partial and final word | |
if (word == fin or chances == 7): | |
break | |
print("") | |
print("") | |
if (word == fin): | |
print("Congrats!!You are a movie buff:)") | |
else: | |
print("HANGMAN :(") | |
print("The movie was ", word) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment