Introductory Python Hangman Game
up vote
-1
down vote
favorite
I'm currently taking a beginners' computer science class for school and we were assigned to create a Hangman game in Python. I've found that after I input a secret word, the program only counts the first letter correctly (also keep in mind, since this is a beginners' class, we were not required to account for other symbols in the secret word). Could somebody review my code and give feedback as to how I can improve it/make it functional? Thanks! Also my apologies for any formatting errors since I have never used StackExchange before.
import sys
#Global Variables
solution =
underscoreWord =
guessedLetters =
missedLetters = 0
def getWord() -> list:
"""Asks the user to enter a word and returns it as a list"""
secretWord = input("Please enter a secret word: ")
secretList = list(secretWord.lower())
return secretList
def makeItUnderscores(secretList:list) -> list:
"""takes in a list with letters and returns the list with underscores"""
global underscoreWord
for i in secretList:
if i.isalpha:
underscoreWord.append("_")
else:
underscoreWord.append(" ")
return underscoreWord
def gameRules(): #defines function
welcome = ['Welcome to Hangman! A word will be chosen at random and',
'you must try to guess the word correctly letter by letter',
'before you run out of attempts. Good luck!'] #Game Rules
for line in welcome:
print (line, sep="n")
def displayFigure(missedLetters:int):
"""Displays the correct figure for the number of letters missed"""
hangman = [
"""
-----
| |
|
|
|
|
|
|
|
--------
""",
"""
-----
| |
| 0
|
|
|
|
|
|
--------
""",
"""
-----
| |
| 0
| -+-
|
|
|
|
|
--------
""",
"""
-----
| |
| 0
| |-+-
|
|
|
|
|
--------
""",
"""
-----
| |
| 0
| |-+-|
|
|
|
|
|
--------
""",
"""
-----
| |
| 0
| |-+-|
| |
| |
|
|
|
--------
""",
"""
-----
| |
| 0
| |-+-|
| |
| |
| |
| |
|
--------
""",
"""
-----
| |
| 0
| |-+-|
| |
| |
| | |
| | |
|
--------
"""]
print (hangman[missedLetters])
def promptForLetter():
"""Asks the user to enter a letter anad processes it."""
global missedLetters
global guessedLetters
global underscoreWord
letterIn = input("Enter a letter: ").lower()
guessedLetters.append(letterIn)
hangmanFlag = False
for index,checkLetter in enumerate (solution):
if letterIn == checkLetter:
hangmanFlag = True
underscoreWord[index] = letterIn
if hangmanFlag == False:
missedLetters += 1
break
def makeScreen():
"""Puts together the parts of the screen"""
global missedLetters
global underscoreWord
global guessedLetters
displayFigure (missedLetters)
print("You need to guess this word:", "".join(underscoreWord))
print("You already used:", "".join(guessedLetters))
def checkForWin():
"""Checks to see if the player has won or lost"""
global solution
global missedLetters
global underscoreWord
if "_" not in underscoreWord:
print()
print()
print("You Win")
sys.exit()
if missedLetters >= 6:
displayFigure (7)
print("Oof, you killed him.")
print("The word was: ", "".join(solution))
sys.exit()
#This is the main program code
print("Welcome to Hangman")
if input("Would you like instructions? (y/n) ") == "y":
gameRules()
solution = getWord()
underscoreWord = makeItUnderscores(solution)
print ("n" * 100)
makeScreen()
while missedLetters < 6:
promptForLetter()
print ("n" * 100)
checkForWin()
makeScreen()
python hangman
New contributor
senpi314 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
up vote
-1
down vote
favorite
I'm currently taking a beginners' computer science class for school and we were assigned to create a Hangman game in Python. I've found that after I input a secret word, the program only counts the first letter correctly (also keep in mind, since this is a beginners' class, we were not required to account for other symbols in the secret word). Could somebody review my code and give feedback as to how I can improve it/make it functional? Thanks! Also my apologies for any formatting errors since I have never used StackExchange before.
import sys
#Global Variables
solution =
underscoreWord =
guessedLetters =
missedLetters = 0
def getWord() -> list:
"""Asks the user to enter a word and returns it as a list"""
secretWord = input("Please enter a secret word: ")
secretList = list(secretWord.lower())
return secretList
def makeItUnderscores(secretList:list) -> list:
"""takes in a list with letters and returns the list with underscores"""
global underscoreWord
for i in secretList:
if i.isalpha:
underscoreWord.append("_")
else:
underscoreWord.append(" ")
return underscoreWord
def gameRules(): #defines function
welcome = ['Welcome to Hangman! A word will be chosen at random and',
'you must try to guess the word correctly letter by letter',
'before you run out of attempts. Good luck!'] #Game Rules
for line in welcome:
print (line, sep="n")
def displayFigure(missedLetters:int):
"""Displays the correct figure for the number of letters missed"""
hangman = [
"""
-----
| |
|
|
|
|
|
|
|
--------
""",
"""
-----
| |
| 0
|
|
|
|
|
|
--------
""",
"""
-----
| |
| 0
| -+-
|
|
|
|
|
--------
""",
"""
-----
| |
| 0
| |-+-
|
|
|
|
|
--------
""",
"""
-----
| |
| 0
| |-+-|
|
|
|
|
|
--------
""",
"""
-----
| |
| 0
| |-+-|
| |
| |
|
|
|
--------
""",
"""
-----
| |
| 0
| |-+-|
| |
| |
| |
| |
|
--------
""",
"""
-----
| |
| 0
| |-+-|
| |
| |
| | |
| | |
|
--------
"""]
print (hangman[missedLetters])
def promptForLetter():
"""Asks the user to enter a letter anad processes it."""
global missedLetters
global guessedLetters
global underscoreWord
letterIn = input("Enter a letter: ").lower()
guessedLetters.append(letterIn)
hangmanFlag = False
for index,checkLetter in enumerate (solution):
if letterIn == checkLetter:
hangmanFlag = True
underscoreWord[index] = letterIn
if hangmanFlag == False:
missedLetters += 1
break
def makeScreen():
"""Puts together the parts of the screen"""
global missedLetters
global underscoreWord
global guessedLetters
displayFigure (missedLetters)
print("You need to guess this word:", "".join(underscoreWord))
print("You already used:", "".join(guessedLetters))
def checkForWin():
"""Checks to see if the player has won or lost"""
global solution
global missedLetters
global underscoreWord
if "_" not in underscoreWord:
print()
print()
print("You Win")
sys.exit()
if missedLetters >= 6:
displayFigure (7)
print("Oof, you killed him.")
print("The word was: ", "".join(solution))
sys.exit()
#This is the main program code
print("Welcome to Hangman")
if input("Would you like instructions? (y/n) ") == "y":
gameRules()
solution = getWord()
underscoreWord = makeItUnderscores(solution)
print ("n" * 100)
makeScreen()
while missedLetters < 6:
promptForLetter()
print ("n" * 100)
checkForWin()
makeScreen()
python hangman
New contributor
senpi314 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Welcome! The Stack Exchange sites have posting guides. You should read those before posting, especially /help/on-topic and /help/how-to-ask
– janos
5 hours ago
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
I'm currently taking a beginners' computer science class for school and we were assigned to create a Hangman game in Python. I've found that after I input a secret word, the program only counts the first letter correctly (also keep in mind, since this is a beginners' class, we were not required to account for other symbols in the secret word). Could somebody review my code and give feedback as to how I can improve it/make it functional? Thanks! Also my apologies for any formatting errors since I have never used StackExchange before.
import sys
#Global Variables
solution =
underscoreWord =
guessedLetters =
missedLetters = 0
def getWord() -> list:
"""Asks the user to enter a word and returns it as a list"""
secretWord = input("Please enter a secret word: ")
secretList = list(secretWord.lower())
return secretList
def makeItUnderscores(secretList:list) -> list:
"""takes in a list with letters and returns the list with underscores"""
global underscoreWord
for i in secretList:
if i.isalpha:
underscoreWord.append("_")
else:
underscoreWord.append(" ")
return underscoreWord
def gameRules(): #defines function
welcome = ['Welcome to Hangman! A word will be chosen at random and',
'you must try to guess the word correctly letter by letter',
'before you run out of attempts. Good luck!'] #Game Rules
for line in welcome:
print (line, sep="n")
def displayFigure(missedLetters:int):
"""Displays the correct figure for the number of letters missed"""
hangman = [
"""
-----
| |
|
|
|
|
|
|
|
--------
""",
"""
-----
| |
| 0
|
|
|
|
|
|
--------
""",
"""
-----
| |
| 0
| -+-
|
|
|
|
|
--------
""",
"""
-----
| |
| 0
| |-+-
|
|
|
|
|
--------
""",
"""
-----
| |
| 0
| |-+-|
|
|
|
|
|
--------
""",
"""
-----
| |
| 0
| |-+-|
| |
| |
|
|
|
--------
""",
"""
-----
| |
| 0
| |-+-|
| |
| |
| |
| |
|
--------
""",
"""
-----
| |
| 0
| |-+-|
| |
| |
| | |
| | |
|
--------
"""]
print (hangman[missedLetters])
def promptForLetter():
"""Asks the user to enter a letter anad processes it."""
global missedLetters
global guessedLetters
global underscoreWord
letterIn = input("Enter a letter: ").lower()
guessedLetters.append(letterIn)
hangmanFlag = False
for index,checkLetter in enumerate (solution):
if letterIn == checkLetter:
hangmanFlag = True
underscoreWord[index] = letterIn
if hangmanFlag == False:
missedLetters += 1
break
def makeScreen():
"""Puts together the parts of the screen"""
global missedLetters
global underscoreWord
global guessedLetters
displayFigure (missedLetters)
print("You need to guess this word:", "".join(underscoreWord))
print("You already used:", "".join(guessedLetters))
def checkForWin():
"""Checks to see if the player has won or lost"""
global solution
global missedLetters
global underscoreWord
if "_" not in underscoreWord:
print()
print()
print("You Win")
sys.exit()
if missedLetters >= 6:
displayFigure (7)
print("Oof, you killed him.")
print("The word was: ", "".join(solution))
sys.exit()
#This is the main program code
print("Welcome to Hangman")
if input("Would you like instructions? (y/n) ") == "y":
gameRules()
solution = getWord()
underscoreWord = makeItUnderscores(solution)
print ("n" * 100)
makeScreen()
while missedLetters < 6:
promptForLetter()
print ("n" * 100)
checkForWin()
makeScreen()
python hangman
New contributor
senpi314 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
I'm currently taking a beginners' computer science class for school and we were assigned to create a Hangman game in Python. I've found that after I input a secret word, the program only counts the first letter correctly (also keep in mind, since this is a beginners' class, we were not required to account for other symbols in the secret word). Could somebody review my code and give feedback as to how I can improve it/make it functional? Thanks! Also my apologies for any formatting errors since I have never used StackExchange before.
import sys
#Global Variables
solution =
underscoreWord =
guessedLetters =
missedLetters = 0
def getWord() -> list:
"""Asks the user to enter a word and returns it as a list"""
secretWord = input("Please enter a secret word: ")
secretList = list(secretWord.lower())
return secretList
def makeItUnderscores(secretList:list) -> list:
"""takes in a list with letters and returns the list with underscores"""
global underscoreWord
for i in secretList:
if i.isalpha:
underscoreWord.append("_")
else:
underscoreWord.append(" ")
return underscoreWord
def gameRules(): #defines function
welcome = ['Welcome to Hangman! A word will be chosen at random and',
'you must try to guess the word correctly letter by letter',
'before you run out of attempts. Good luck!'] #Game Rules
for line in welcome:
print (line, sep="n")
def displayFigure(missedLetters:int):
"""Displays the correct figure for the number of letters missed"""
hangman = [
"""
-----
| |
|
|
|
|
|
|
|
--------
""",
"""
-----
| |
| 0
|
|
|
|
|
|
--------
""",
"""
-----
| |
| 0
| -+-
|
|
|
|
|
--------
""",
"""
-----
| |
| 0
| |-+-
|
|
|
|
|
--------
""",
"""
-----
| |
| 0
| |-+-|
|
|
|
|
|
--------
""",
"""
-----
| |
| 0
| |-+-|
| |
| |
|
|
|
--------
""",
"""
-----
| |
| 0
| |-+-|
| |
| |
| |
| |
|
--------
""",
"""
-----
| |
| 0
| |-+-|
| |
| |
| | |
| | |
|
--------
"""]
print (hangman[missedLetters])
def promptForLetter():
"""Asks the user to enter a letter anad processes it."""
global missedLetters
global guessedLetters
global underscoreWord
letterIn = input("Enter a letter: ").lower()
guessedLetters.append(letterIn)
hangmanFlag = False
for index,checkLetter in enumerate (solution):
if letterIn == checkLetter:
hangmanFlag = True
underscoreWord[index] = letterIn
if hangmanFlag == False:
missedLetters += 1
break
def makeScreen():
"""Puts together the parts of the screen"""
global missedLetters
global underscoreWord
global guessedLetters
displayFigure (missedLetters)
print("You need to guess this word:", "".join(underscoreWord))
print("You already used:", "".join(guessedLetters))
def checkForWin():
"""Checks to see if the player has won or lost"""
global solution
global missedLetters
global underscoreWord
if "_" not in underscoreWord:
print()
print()
print("You Win")
sys.exit()
if missedLetters >= 6:
displayFigure (7)
print("Oof, you killed him.")
print("The word was: ", "".join(solution))
sys.exit()
#This is the main program code
print("Welcome to Hangman")
if input("Would you like instructions? (y/n) ") == "y":
gameRules()
solution = getWord()
underscoreWord = makeItUnderscores(solution)
print ("n" * 100)
makeScreen()
while missedLetters < 6:
promptForLetter()
print ("n" * 100)
checkForWin()
makeScreen()
python hangman
python hangman
New contributor
senpi314 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
senpi314 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
senpi314 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked 5 hours ago
senpi314
1
1
New contributor
senpi314 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
senpi314 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
senpi314 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Welcome! The Stack Exchange sites have posting guides. You should read those before posting, especially /help/on-topic and /help/how-to-ask
– janos
5 hours ago
add a comment |
Welcome! The Stack Exchange sites have posting guides. You should read those before posting, especially /help/on-topic and /help/how-to-ask
– janos
5 hours ago
Welcome! The Stack Exchange sites have posting guides. You should read those before posting, especially /help/on-topic and /help/how-to-ask
– janos
5 hours ago
Welcome! The Stack Exchange sites have posting guides. You should read those before posting, especially /help/on-topic and /help/how-to-ask
– janos
5 hours ago
add a comment |
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "196"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
senpi314 is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f209713%2fintroductory-python-hangman-game%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
senpi314 is a new contributor. Be nice, and check out our Code of Conduct.
senpi314 is a new contributor. Be nice, and check out our Code of Conduct.
senpi314 is a new contributor. Be nice, and check out our Code of Conduct.
senpi314 is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f209713%2fintroductory-python-hangman-game%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Welcome! The Stack Exchange sites have posting guides. You should read those before posting, especially /help/on-topic and /help/how-to-ask
– janos
5 hours ago