Skip to content

Commit

Permalink
Merge pull request #1314 from shivamadolia/patch-4
Browse files Browse the repository at this point in the history
Update Hangman.py
  • Loading branch information
fineanmol authored Nov 2, 2021
2 parents b56b14e + 633b369 commit 3edfec8
Showing 1 changed file with 119 additions and 59 deletions.
178 changes: 119 additions & 59 deletions Program's_Contributed_By_Contributors/Python_Programs/Hangman.py
Original file line number Diff line number Diff line change
@@ -1,65 +1,125 @@

# Classic Game of Hangman
# Created by Santosh Vasisht
# Improved version by Shivam Adolia

import sys

import random

# Initialization
fruit_list = [
'apple',
'banana',
'orange',
'mango',
'strawberry',
'watermelon',
'kiwi',
'peach',
'pear',
'papaya',
'grapes',
'pineapple',
'guava',
'blueberry',
'blackberry',
'raspberry',
'apricot']

choice = 'Y'

# Game Start
while choice == 'Y':
#Setup
fruit = random.choice(fruit_list)
lives = 5
temp = []
word = []

for i in fruit:
word.append(i)
temp.append('_')

print("\n\tHANGMAN!")

#Gameplay
while '_' in temp and lives!=0:
print() #newline
for i in temp:
print(i, end = ' ')
print("\t Lives:", lives, '\n')
l = input("Guess a letter: ").lower()

if l in word:
for i in range(0,len(word)):
if word[i] == l:
temp[i] = l


name = input("What is your name?\n")



def run():

print(f"Hello, {name}, let's play hangman!")



print("Start guessing...")



wordbank = ["avocado", "chicken", "funds", "bankrupt", "cushion", "filming", "apartment", "radio", "detective", "vinegar", "curtains", "carpet", "addictive", "control", "raining", "sunshine", "diamond", "charcoal", "chocolate", "container", "cubes", "fan", "processor", "sunbed", "towel", "jellyfish", "meals"]



word = random.choice(wordbank)



guesses = " "



turns = 10



while turns > 0:

failed = 0

for char in word:

if char in guesses:

print(char)

else:

print("_")

failed += 1



if failed == 0:

print("You win!")



choice = input("Would you like to play again? y/n\n")



if "y" in choice:

run()

elif "n" in choice:

sys.exit()

else:

print("Something went wrong, type y or n.")





guess = input("Guess a character:")

guesses += guess



if guess not in word:

turns -= 1

print("Wrong!")

print(f"You have {turns} more guesses.")



if turns == 0:

print("You lose!")



choice = input("Would you like to play again? y/n\n")



if "y" in choice:

run()

elif "n" in choice:

sys.exit()

else:
lives -= 1

# Game End
if lives == 0:
print("\nGAME OVER!")
else:
print("\nYOU WIN!!!")
print("The word was", fruit.upper())

choice = input("\nPlay Again?(y/n) ").upper()

print("Something went wrong, type y or n.")



run()

0 comments on commit 3edfec8

Please sign in to comment.