Advertisement
Guest User

Untitled

a guest
Feb 24th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.05 KB | None | 0 0
  1. import random
  2. from urllib.request import urlopen
  3.  
  4. def finding_words():
  5.     data = bytes.decode(urlopen('http://www.talkenglish.com/vocabulary/top-1500-nouns.aspx').read())
  6.     a = data.split('''"''')
  7.     list1 = []
  8.     list2 = []
  9.     for data in a:
  10.         if data[0:14] == "http://www.eng":
  11.             list1.append(data)
  12.     for i in list1:
  13.         list2.append(i.split("/")[4])
  14.     return list2
  15.  
  16. def hangman(wordlist):
  17.     tries = input("How many tries do you think you'll need?: ")
  18.     wordlist1 = wordlist
  19.     selection = wordlist1[random.randrange(len(wordlist1))]
  20.     hidden_word = [n for n in selection]
  21.     list1 = ["_"] * len(hidden_word)
  22.     correct = 0
  23.     tries_left = int(tries)
  24.     print(''.join(list1))
  25.     used_letter = []
  26.     hint1 = False
  27.     while correct < len(hidden_word):
  28.         while tries_left == int(tries)//2 and hint1 == False:
  29.             hint = input("do you need a free letter? yes/no: ")
  30.             if hint == "yes":
  31.                 hint1 = True
  32.                 for i in ''.join(random.sample(selection, len(selection))):
  33.                     if i not in list1:
  34.                         x = i
  35.                 free_position = [pos1 for pos1, char1 in enumerate(hidden_word) if char1 is x]
  36.                 for i, j in enumerate(hidden_word):
  37.                     if i in free_position:
  38.                         list1[i] = j
  39.                         correct += 1
  40.                         if list1 == hidden_word:
  41.                             print(''.join(list1))
  42.                             again = input("Congratulations! you won! try again? yes/no: ")
  43.                             if again == "yes":
  44.                                 hangman(wordlist)
  45.                             if again is not "yes":
  46.                                 return
  47.                 print(''.join(list1))
  48.             if hint == "no":
  49.                 hint1 = True
  50.                 pass
  51.         if tries_left == 0:
  52.             print("The hidden word was: ", ''.join(hidden_word))
  53.             again = input("you lost! try again? yes/no: ")
  54.             if again == "yes":
  55.                 hangman(wordlist)
  56.             if again is not "yes":
  57.                 break
  58.         letter = input("letter or full word: ")
  59.         if len(letter) is not 1 and len(letter) is not len(hidden_word):
  60.             print("Only one letter or full word is allowed")
  61.             continue
  62.         if letter in list1:
  63.             tries_left += -1
  64.             print("You already guessed this letter correctly. Tries left: ", tries_left)
  65.         if letter in hidden_word and letter not in list1:
  66.             positie = [pos for pos, char in enumerate(hidden_word) if char is letter]
  67.             for index, item in enumerate(hidden_word):
  68.                 if index in positie:
  69.                     list1[index] = item
  70.                     correct += 1
  71.             print(''.join(list1))
  72.         if len(letter) == len(hidden_word):
  73.             if letter == selection:
  74.                 print(selection)
  75.                 again = input("Congratulations! you won! try again? yes/no: ")
  76.                 if again == "yes":
  77.                     hangman(wordlist)
  78.                 if again is not "yes":
  79.                     return
  80.             if letter is not selection:
  81.                 tries_left += -1
  82.                 print("That is not correct. Tries left: ", tries_left)
  83.         elif letter not in hidden_word:
  84.             tries_left += -1
  85.             if letter in used_letter:
  86.                 print("You used that letter already, you still lose a try though")
  87.             if letter not in used_letter:
  88.                 used_letter.append(letter)
  89.                 used_letter.sort()
  90.             print(''.join(list1))
  91.             print("That letter is not in the word. Tries left: ",tries_left)
  92.             print("Your used letters are: ", ','.join(used_letter))
  93.     else:
  94.         again = input("Congratulations! you won! try again? yes/no: ")
  95.         if again == "yes":
  96.             hangman(wordlist)
  97.         if again is not "yes":
  98.             return
  99.  
  100. wordlist = finding_words()
  101. hangman(wordlist)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement