Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- #choose a word
- def chooseRandomWord():
- words=[]
- with open('sowpods.txt', 'r') as file:
- line = file.readline()
- while line:
- words.append(line.replace("\n","".strip()))
- line = file.readline()
- choice=words[random.randint(0,len(words)-1)]
- return choice
- #start game
- print("Welcome to Hangman!")
- #pick the word
- secretWord=chooseRandomWord()
- #get word in a list
- dashes=list(secretWord)
- displayList=[]
- #get dashes for all the letters
- for i in dashes:
- displayList.append("_")
- count=len(secretWord)
- guesses=0
- letter = 0
- usedList=[]
- while count != 0 and letter != "exit":
- print(" ".join(displayList))
- letter=input("Guess your letter: ")
- if letter.upper() in usedList:
- print("Oops! Already guessed that letter.")
- else:
- for i in range(0,len(secretWord)):
- if letter.upper() == secretWord[i]:
- displayList[i]=letter.upper()
- count -= 1
- guesses +=1
- usedList.append(letter.upper())
- print("You've used these letters so far: ", usedList)
- if letter == "exit":
- print("Thanks!")
- else:
- print(" ".join(displayList))
- print("Good job! You figured that the word is "+secretWord+" after guessing "+str(guesses)+" letters!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement