Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Hangman.py #1314

Merged
merged 1 commit into from
Nov 2, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()