-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1314 from shivamadolia/patch-4
Update Hangman.py
- Loading branch information
Showing
1 changed file
with
119 additions
and
59 deletions.
There are no files selected for viewing
178 changes: 119 additions & 59 deletions
178
Program's_Contributed_By_Contributors/Python_Programs/Hangman.py
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
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() |