Advertisement
Guest User

Guess the word

a guest
Nov 23rd, 2017
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1. import random
  2.  
  3. #---------------------------lists and variables-------------------
  4. words=["apple","programming","christmas","Hangman"]
  5. word=random.choice(words)
  6. tries=0
  7. guessed_letters=''
  8. #---------------------------Functions-----------------------------
  9. def check(guess):
  10. if guess.lower()==word.lower():
  11. print("Correct! It only took you %s tries." % tries)
  12. elif len(guess)==0 or len(guess)>=2:
  13. print("Invalid.This is not counted as a try.")
  14. main()
  15. elif len(guess)== len(word):
  16. print("Nice try, but that was not the word:")
  17. tries+=1
  18. main()
  19. elif guess.isalpha():
  20. global guessed_letters
  21. if guessed_letters.find(guess)>-1:
  22. print("You have already guessed that letter.")
  23. main()
  24. else:
  25. guessed_letters= guessed_letters+guess
  26. pass
  27. else:
  28. print("Invalid.This is not counted as a try.")
  29. main()
  30. def board():
  31. for letter in word:
  32. if guessed_letters.find(letter)>-1:
  33. print(letter, end=" ")
  34. else:
  35. print("-",end=" ")
  36. print()
  37.  
  38.  
  39. def main():
  40. global tries
  41. while True:
  42. if tries>=10:
  43. print("You got hung! The word was %s." % word)
  44. break
  45. board()
  46. print("Tries you took out of 10:%s" % tries)
  47. print("The word contains %s letters." % len(word))
  48. guess=input("Guess a letter or the word itself:")
  49. check(guess)
  50. tries+=1
  51.  
  52.  
  53. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement