How to Build a Hangman Game with Python

preview_player
Показать описание

In this video, CBT Nuggets trainer Ben Finkel covers a great way to practice coding in Python: developing your own simple game.

Once you've learned loops and conditionals in Python, you can master them and other skills on variables, data types and operators by developing a Hangman game. Ben walks you through implementing the rudimentary, early pieces of coding and programming in Python and combining them in interesting ways. Practice your Python skills: Ben covers loops, complex if statements and checks, and breaks. The game Hangman can be a bit morbid, but learning Python doesn't have to be.

Start learning with CBT Nuggets:

Рекомендации по теме
Комментарии
Автор

I like how your code is beginner friendly! Very helpful.

anjaniaditisingh.
Автор

Thanks for the game. I added a few lines:
#The secret word the player is trying to guess
import random
Let's play Hangman
#word list: can be expanded
wbase = ['birthday', 'background', 'children', 'people', 'orange', 'station', 'Saturday',
'computer', 'January', 'office', 'helicopter', 'channel', 'stadium', 'friendship', 'interesting']

chosenWord = random.choice(wbase)
secretWord = chosenWord.upper()
letterGuessed = ''

#The number of turns before the player loses
failureCount = 6

#Loop until the player has made to many failed attempts
#Will 'break' loop if they succeed instead
while failureCount > 0:

#Get the guessed letter from the player
playerGuess = input('\nEnter a letter: ')
guess = playerGuess.upper()

if guess in secretWord:
#Player guessed a correct letter!
print(f'Correct. There is one or more {guess} in the secret word.')
else:
failureCount -= 1
print(f'Incorrect. There are no {guess} in the secretWord.{failureCount} turns left')

#Maintain a list of all letters guessed
letterGuessed += guess
wrongLetterCount = 0

for letter in secretWord:
if letter in letterGuessed:
print(f'{letter}', end = '')
else:
print('_', end = '')
wrongLetterCount +=1
print("")
#If there we no wrong letters, then the player won.
if wrongLetterCount == 0:
print(f'\nCongratulations! The secretWord word was {secretWord}!')
break

else:
print("\nSorry, you didn't win it. Better luck next time.")#Hoping it can be of help for beginners like me.

franmontero
Автор

great tutorial I belived that it can't be done without set or enumerate even list or dictionary comprenhensions, thank you, you help me a lot

davidfelipe
Автор

3:35 You can put a underscore between the word to make it look nicers (lines 1 and 2) Letters_Guessed
Secret_Word

noahporter
Автор

Can you put this code in the description so I can copy and paste. I copy everything that you did but it’s still wrong.

l___
Автор

Thank you. Really helpful for a beginner like me...

mdneazali
Автор

thank you so much, I created the same code without problems on visual studio but i keep getting this error:SyntaxError: unexpected character after line continuation character, i checked for any backslashes and i found none, an help?

alyatalla
Автор

Great tutorial with nice explaining + pacing

lakostal
Автор

Great video. But I just wanna know is there are any way to randomly generate english words or fetch from internet?

vilashegde
Автор

this is alittle bit complicated to me but am gonna try

Alaaja_aj
Автор

I didn't know you can use else eith while. I've always known it as a construct used only for if statements

seetsamolapo
Автор

Hi! Great tutorial! Would ove some help with a proyect I got :( Can't get the hang of it.

adrianrojasstanley
Автор

This doesn't run properly, not sure why. maybe this is just a demo code or something.

friday
Автор

Can you legally just say 'like' Wheel of Fortune?

ejohnson