Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Written for Python 3.6
- # Playable in the Python 3.6.1 Shell
- # To play put the files:
- # "google-10000-english-usa-no-swears-long.txt"
- # "google-10000-english-usa-no-swears-medium.txt"
- # "google-10000-english-usa-no-swears-short.txt"
- # in your Python36 folder
- # To start use: exec(open("./Hangman.py").read())
- # Start of the progam
- print ('Welcome to Hangman!')
- start = input('Press enter to continue...\n')
- import random
- alphabet = list('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ') # to be used as a check that a letter is entered
- allwords = []
- if start != '':
- print('That is not the enter key..')
- start = input("Press enter to continue, any other key will exit the program.\n")
- if start != '':
- print ('Exiting program')
- quit()
- #DIFFICULTY
- print ('On what difficulty do you want to play?\n')
- difficulty = input('1.Easy\n2.Medium\n3.Hard\n\n')
- # diffvalues = ['1','2','3','easy','Easy','EASY','medium','Medium','MEDIUM','hard','Hard','HARD']
- # while difficulty not in diffvalues:
- ## Wat als ik nou EaSy wil doen? Met de lower functie kan je een string in lowercase veranderen!
- while difficulty.lower() not in ['1','2','3','easy','medium','hard']:
- print ('Sorry, that was not one of the choices')
- print ('On what difficulty do you want to play?\n')
- difficulty = input('1.Easy\n2.Medium\n3.Hard\n\n')
- # if difficulty == '1' or difficulty == 'Easy' or difficulty == 'easy' or difficulty == 'EASY':
- if difficulty.lower() in ['1', 'easy']:
- with open('google-10000-english-usa-no-swears-short.txt') as inputfile:
- for line in inputfile:
- allwords.extend(line.strip().split(','))
- # elif difficulty == '2' or difficulty == 'Medium' or difficulty == 'medium' or difficulty == 'MEDIUM':
- elif difficulty.lower() in ['2', 'medium']:
- with open('google-10000-english-usa-no-swears-medium.txt') as inputfile:
- for line in inputfile:
- allwords.extend(line.strip().split(','))
- # elif difficulty == '3' or difficulty == 'Hard' or difficulty == 'hard' or difficulty == 'HARD':
- elif difficulty.lower() in ['3', 'hard']:
- with open('google-10000-english-usa-no-swears-long.txt') as inputfile:
- for line in inputfile:
- allwords.extend(line.strip().split(','))
- # In bovenstaande stuk heb je wel drie keer precies hetzelfde gedaan om een bestand uit te lezen. Dit zou je ook kunnen veranden in een functie bijv:
- def readFile(filename):
- wordsInFile = []
- with open(filename) as inputfile:
- for line in inputfile:
- wordsInFile.extemd(line.strip().split(','))
- return wordsInFile
- print ("All right then, let's start.\n")
- # GAME VARIABLES
- words = ['apple', 'banana', 'coconut'] # to test it in a webinterpreter, change word = random.choice(allwords) into word = random.choice(words)
- word = random.choice(allwords) # random selection from list
- #print (word) # for testing, this is the word chosen, needs to be removed for the actual game
- length = len(word) # To determine the number of underscores in uslist
- uslist = ['_'] * length # A list with as much underscores as letters in the word
- hangword = list(word) # The randomly selected word as a list
- guessesleft = 8 # Amount of guesses
- sentence1 = ('You have %d guesses!\n') % guessesleft # Standard sentence used later on to count
- print ('')
- print('The word looks like this:\n')
- print (' '.join(uslist))
- print ('')
- print (sentence1)
- print ('')
- # Start of the game
- playerguess = input('Guess a letter..\n')
- playerguesslow = playerguess.lower()
- while playerguesslow not in alphabet or len(playerguess) > 1 or playerguess == '': # Prevents the player from entering non-letters, multiple letters or enter
- playerguess = input ('That is not a letter, please try again.\n')
- playerguesslow = playerguess.lower()
- while guessesleft > 1 :
- if playerguesslow in word:
- for n,i in enumerate(hangword):
- if i==playerguesslow:
- uslist[n]=playerguesslow
- rightletter = 'Letter "%s" is correct!\n' % playerguess
- print (rightletter)
- print('The word now looks like this:\n ')
- print (' '.join(uslist))
- print ('')
- sentence3 = 'You still have %d guesses left.\n' % guessesleft
- if uslist != hangword:
- print (sentence3)
- if uslist == hangword:
- print ('Congratulations..\n')
- break
- else:
- playerguess = input('Guess a letter..\n')
- playerguesslow = playerguess.lower()
- while playerguesslow not in alphabet or len(playerguess) > 1 or playerguess == '':
- playerguess = input ('That is not a letter, please try again.\n')
- playerguesslow = playerguess.lower()
- while playerguesslow in uslist:
- playerguess = input ('You have already chosen that letter.\n')
- playerguesslow = playerguess.lower()
- else:
- guessesleft = guessesleft-1
- wrongletter = 'There is no letter "%s" in the word.' % playerguess
- print (wrongletter)
- sentence2 = 'Too bad, you have %d guesses left..\n' % guessesleft
- print (sentence2)
- print('The word still looks like this:\n ')
- print (' '.join(uslist))
- print ('')
- playerguess = input('Guess again..\n')
- playerguesslow = playerguess.lower()
- while playerguesslow not in alphabet or len(playerguess) > 1 or playerguess == '':
- playerguess = input ('That is not a letter, please try again.\n')
- playerguesslow = playerguess.lower()
- while playerguesslow in uslist:
- playerguess = input ('You have already chosen that letter.\n')
- playerguesslow = playerguess.lower()
- # End of the game
- if uslist == hangword:
- print ('You have won!\n')
- else:
- print ('I am sorry, you are game over..\n')
- wordnotfound = 'the word you were looking for was "%s".\n' % word
- print (wordnotfound)
- print ('Would you like to play another game?\n')
- qnewgame = input ('Yes or No ?\n')
- if qnewgame == 'Yes' or qnewgame == 'yes'or qnewgame == 'YES' or qnewgame == 'Y' or qnewgame == 'y':
- exec(open("./Hangman.py").read())
- elif qnewgame == 'No'or qnewgame == 'no'or qnewgame == 'NO' or qnewgame == 'N' or qnewgame == 'n':
- quit()
- else:
- qnewgame = input ('I did not quite get that. Yes or No?')
- if qnewgame == 'Yes' or qnewgame == 'yes'or qnewgame == 'YES' or qnewgame == 'Y' or qnewgame == 'y':
- exec(open("./Hangman.py").read())
- elif qnewgame == 'No'or qnewgame == 'no'or qnewgame == 'NO':
- quit()
- else:
- print ('Sorry, I still did not get that, the program will now quit.')
- print ('')
- quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement