Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- from urllib.request import urlopen
- def finding_words():
- data = bytes.decode(urlopen('http://www.talkenglish.com/vocabulary/top-1500-nouns.aspx').read())
- a = data.split('''"''')
- list1 = []
- list2 = []
- for data in a:
- if data[0:14] == "http://www.eng":
- list1.append(data)
- for i in list1:
- list2.append(i.split("/")[4])
- return list2
- def hangman(wordlist):
- tries = input("How many tries do you think you'll need?: ")
- wordlist1 = wordlist
- selection = wordlist1[random.randrange(len(wordlist1))]
- hidden_word = [n for n in selection]
- list1 = ["_"] * len(hidden_word)
- correct = 0
- tries_left = int(tries)
- print(''.join(list1))
- used_letter = []
- hint1 = False
- while correct < len(hidden_word):
- while tries_left == int(tries)//2 and hint1 == False:
- hint = input("do you need a free letter? yes/no: ")
- if hint == "yes":
- hint1 = True
- for i in ''.join(random.sample(selection, len(selection))):
- if i not in list1:
- x = i
- free_position = [pos1 for pos1, char1 in enumerate(hidden_word) if char1 is x]
- for i, j in enumerate(hidden_word):
- if i in free_position:
- list1[i] = j
- correct += 1
- if list1 == hidden_word:
- print(''.join(list1))
- again = input("Congratulations! you won! try again? yes/no: ")
- if again == "yes":
- hangman(wordlist)
- if again is not "yes":
- return
- print(''.join(list1))
- if hint == "no":
- hint1 = True
- pass
- if tries_left == 0:
- print("The hidden word was: ", ''.join(hidden_word))
- again = input("you lost! try again? yes/no: ")
- if again == "yes":
- hangman(wordlist)
- if again is not "yes":
- break
- letter = input("letter or full word: ")
- if len(letter) is not 1 and len(letter) is not len(hidden_word):
- print("Only one letter or full word is allowed")
- continue
- if letter in list1:
- tries_left += -1
- print("You already guessed this letter correctly. Tries left: ", tries_left)
- if letter in hidden_word and letter not in list1:
- positie = [pos for pos, char in enumerate(hidden_word) if char is letter]
- for index, item in enumerate(hidden_word):
- if index in positie:
- list1[index] = item
- correct += 1
- print(''.join(list1))
- if len(letter) == len(hidden_word):
- if letter == selection:
- print(selection)
- again = input("Congratulations! you won! try again? yes/no: ")
- if again == "yes":
- hangman(wordlist)
- if again is not "yes":
- return
- if letter is not selection:
- tries_left += -1
- print("That is not correct. Tries left: ", tries_left)
- elif letter not in hidden_word:
- tries_left += -1
- if letter in used_letter:
- print("You used that letter already, you still lose a try though")
- if letter not in used_letter:
- used_letter.append(letter)
- used_letter.sort()
- print(''.join(list1))
- print("That letter is not in the word. Tries left: ",tries_left)
- print("Your used letters are: ", ','.join(used_letter))
- else:
- again = input("Congratulations! you won! try again? yes/no: ")
- if again == "yes":
- hangman(wordlist)
- if again is not "yes":
- return
- wordlist = finding_words()
- hangman(wordlist)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement