HangMan game in PYTHON
What is hangman? Libraries
Code:
import random as r
listOfWords1 = ["pace", "space", "cross", "edge", "nine", "tame", "spawn", "real", "world", "seed"]
listOfWords2 = ["computer", "physics", "science", "sapiens", "building"]
def main():
guesses = ""
level = input("Easy(e)/Medium(m)/Hard(h):\n").lower()
print("----------")
if level == "e":
turns = 5
word = r.choice(listOfWords1)
elif level == "m":
turns = 3
word = r.choice(listOfWords1)
elif level == "h":
turns = 3
word = r.choice(listOfWords2)
else:
print(f" {level} is not a valid command, please try 'e' for easy, 'm' for medium and 'h' for hard level respectively. ")
main()
while turns > 0:
failed = 0
for char in word:
if char in guesses:
print(char)
else:
print("-")
failed += 1
if failed == 0:
print("you won")
ask = input("Do you want to play again? (y/n):\n").lower()
if ask == "y":
main()
else:
break
guess = input("::\n").lower()
guesses += guess
print("----------")
if guess not in word:
turns -= 1
print(f"You have {turns} more guesses. ")
if turns == 0:
print("you lost!!")
print(f"The word was '{word}'. ")
ask = input("Do you want to play again?(y/n)\n").lower()
if ask == "y":
main()
else:
break
if __name__ == "__main__":
main()
Comments
Post a Comment