First Hangman game in Python 3.6 [closed]
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
1
down vote
favorite
Should I create a main function, and is everything placed right or should I def
more or fewer functions?
import random
# open text file with a word on every new line
# [:-1] removes n, from behind the word, wich is also pulled from the text
with open('sowpods.txt', 'r') as words:
chosen_word = (random.choice(words.readlines()))[:-1]
right_guess = set()
wrong_guess = set()
lives = ['1up', '2up', '3up', '4up', '5up', '6up']
print('Welcome to hangman.nnEnter a letter to start playing, you have six lives.nGoodluck!')
def progress():
# for every character in chosen_word
for i in chosen_word:
# see if the character is in the players guess
if i in right_guess:
# print then out the character
print(i, end=' ')
elif not i in right_guess:
# if not found, print a dash
print('_', end=' ')
def start():
while True:
progress()
# check if the word has been guessed yet
if right_guess == set(chosen_word):
print('nnnCongratulations you've won!')
# forces the userinput to uppercase
x = input('nn').upper()
# check if the letter has already been guessed right
if x in right_guess:
print('You've already guessed this letter!')
# collects all the right guessed letters
elif x in chosen_word:
right_guess.add(x)
print('correctn')
# check if the letter has already been guessed wrong
elif x in wrong_guess:
print('You've already tried this letter.')
# collect wrong guesses and checks if input is a single entry
elif not x in chosen_word and len(x) == 1:
wrong_guess.add(x)
print('wrong')
# remove a live from the list when wrong guess
del lives[0]
# if all lives gone end game
if len(lives) > 0:
print('Lives left:n' + str(len(lives)))
elif len(lives) <= 0:
print('You have no more lives left, restart to try again')
print('Maby you'll get an easier word then:')
print(chosen_word)
print('Good luck')
# for when input isnt a singe entry
else:
print('You must enter a singel letter.')
print(start())
python python-3.x hangman
closed as off-topic by Daniel, Stephen Rauch, Sam Onela, Mast, Graipher May 12 at 20:57
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." â Daniel, Stephen Rauch, Sam Onela, Mast, Graipher
 |Â
show 1 more comment
up vote
1
down vote
favorite
Should I create a main function, and is everything placed right or should I def
more or fewer functions?
import random
# open text file with a word on every new line
# [:-1] removes n, from behind the word, wich is also pulled from the text
with open('sowpods.txt', 'r') as words:
chosen_word = (random.choice(words.readlines()))[:-1]
right_guess = set()
wrong_guess = set()
lives = ['1up', '2up', '3up', '4up', '5up', '6up']
print('Welcome to hangman.nnEnter a letter to start playing, you have six lives.nGoodluck!')
def progress():
# for every character in chosen_word
for i in chosen_word:
# see if the character is in the players guess
if i in right_guess:
# print then out the character
print(i, end=' ')
elif not i in right_guess:
# if not found, print a dash
print('_', end=' ')
def start():
while True:
progress()
# check if the word has been guessed yet
if right_guess == set(chosen_word):
print('nnnCongratulations you've won!')
# forces the userinput to uppercase
x = input('nn').upper()
# check if the letter has already been guessed right
if x in right_guess:
print('You've already guessed this letter!')
# collects all the right guessed letters
elif x in chosen_word:
right_guess.add(x)
print('correctn')
# check if the letter has already been guessed wrong
elif x in wrong_guess:
print('You've already tried this letter.')
# collect wrong guesses and checks if input is a single entry
elif not x in chosen_word and len(x) == 1:
wrong_guess.add(x)
print('wrong')
# remove a live from the list when wrong guess
del lives[0]
# if all lives gone end game
if len(lives) > 0:
print('Lives left:n' + str(len(lives)))
elif len(lives) <= 0:
print('You have no more lives left, restart to try again')
print('Maby you'll get an easier word then:')
print(chosen_word)
print('Good luck')
# for when input isnt a singe entry
else:
print('You must enter a singel letter.')
print(start())
python python-3.x hangman
closed as off-topic by Daniel, Stephen Rauch, Sam Onela, Mast, Graipher May 12 at 20:57
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." â Daniel, Stephen Rauch, Sam Onela, Mast, Graipher
2
Your code is not indented properly as shown. Please edit your post, simply copy from your editor, paste in the body of your post, then select all your code and press Ctrl+K / Command+K (Mac).
â Phrancis
May 12 at 5:51
Can you add an excerpt of sowpods.txt? Your code is broken, by the way. Did you test it?
â Daniel
May 12 at 9:25
@Coal_ its a text document on my computer, all it does is generate a random word, the code runs fine. I'm not sure if there is a way i can include it on codereview
â Sjoerd Gerritsen
May 12 at 10:07
The code runs, but it doesn't work as expected (hint: you're missing a call tochosen_word.upper()
).
â Daniel
May 12 at 10:30
@Sjoerd, I think it's oke to add an excerpt ofsowpods.txt
reduced to a few tens of lines to the question.
â Jan Kuiken
May 12 at 16:15
 |Â
show 1 more comment
up vote
1
down vote
favorite
up vote
1
down vote
favorite
Should I create a main function, and is everything placed right or should I def
more or fewer functions?
import random
# open text file with a word on every new line
# [:-1] removes n, from behind the word, wich is also pulled from the text
with open('sowpods.txt', 'r') as words:
chosen_word = (random.choice(words.readlines()))[:-1]
right_guess = set()
wrong_guess = set()
lives = ['1up', '2up', '3up', '4up', '5up', '6up']
print('Welcome to hangman.nnEnter a letter to start playing, you have six lives.nGoodluck!')
def progress():
# for every character in chosen_word
for i in chosen_word:
# see if the character is in the players guess
if i in right_guess:
# print then out the character
print(i, end=' ')
elif not i in right_guess:
# if not found, print a dash
print('_', end=' ')
def start():
while True:
progress()
# check if the word has been guessed yet
if right_guess == set(chosen_word):
print('nnnCongratulations you've won!')
# forces the userinput to uppercase
x = input('nn').upper()
# check if the letter has already been guessed right
if x in right_guess:
print('You've already guessed this letter!')
# collects all the right guessed letters
elif x in chosen_word:
right_guess.add(x)
print('correctn')
# check if the letter has already been guessed wrong
elif x in wrong_guess:
print('You've already tried this letter.')
# collect wrong guesses and checks if input is a single entry
elif not x in chosen_word and len(x) == 1:
wrong_guess.add(x)
print('wrong')
# remove a live from the list when wrong guess
del lives[0]
# if all lives gone end game
if len(lives) > 0:
print('Lives left:n' + str(len(lives)))
elif len(lives) <= 0:
print('You have no more lives left, restart to try again')
print('Maby you'll get an easier word then:')
print(chosen_word)
print('Good luck')
# for when input isnt a singe entry
else:
print('You must enter a singel letter.')
print(start())
python python-3.x hangman
Should I create a main function, and is everything placed right or should I def
more or fewer functions?
import random
# open text file with a word on every new line
# [:-1] removes n, from behind the word, wich is also pulled from the text
with open('sowpods.txt', 'r') as words:
chosen_word = (random.choice(words.readlines()))[:-1]
right_guess = set()
wrong_guess = set()
lives = ['1up', '2up', '3up', '4up', '5up', '6up']
print('Welcome to hangman.nnEnter a letter to start playing, you have six lives.nGoodluck!')
def progress():
# for every character in chosen_word
for i in chosen_word:
# see if the character is in the players guess
if i in right_guess:
# print then out the character
print(i, end=' ')
elif not i in right_guess:
# if not found, print a dash
print('_', end=' ')
def start():
while True:
progress()
# check if the word has been guessed yet
if right_guess == set(chosen_word):
print('nnnCongratulations you've won!')
# forces the userinput to uppercase
x = input('nn').upper()
# check if the letter has already been guessed right
if x in right_guess:
print('You've already guessed this letter!')
# collects all the right guessed letters
elif x in chosen_word:
right_guess.add(x)
print('correctn')
# check if the letter has already been guessed wrong
elif x in wrong_guess:
print('You've already tried this letter.')
# collect wrong guesses and checks if input is a single entry
elif not x in chosen_word and len(x) == 1:
wrong_guess.add(x)
print('wrong')
# remove a live from the list when wrong guess
del lives[0]
# if all lives gone end game
if len(lives) > 0:
print('Lives left:n' + str(len(lives)))
elif len(lives) <= 0:
print('You have no more lives left, restart to try again')
print('Maby you'll get an easier word then:')
print(chosen_word)
print('Good luck')
# for when input isnt a singe entry
else:
print('You must enter a singel letter.')
print(start())
python python-3.x hangman
edited May 12 at 8:08
Daniel
4,1132836
4,1132836
asked May 12 at 5:29
Sjoerd Gerritsen
64
64
closed as off-topic by Daniel, Stephen Rauch, Sam Onela, Mast, Graipher May 12 at 20:57
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." â Daniel, Stephen Rauch, Sam Onela, Mast, Graipher
closed as off-topic by Daniel, Stephen Rauch, Sam Onela, Mast, Graipher May 12 at 20:57
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." â Daniel, Stephen Rauch, Sam Onela, Mast, Graipher
2
Your code is not indented properly as shown. Please edit your post, simply copy from your editor, paste in the body of your post, then select all your code and press Ctrl+K / Command+K (Mac).
â Phrancis
May 12 at 5:51
Can you add an excerpt of sowpods.txt? Your code is broken, by the way. Did you test it?
â Daniel
May 12 at 9:25
@Coal_ its a text document on my computer, all it does is generate a random word, the code runs fine. I'm not sure if there is a way i can include it on codereview
â Sjoerd Gerritsen
May 12 at 10:07
The code runs, but it doesn't work as expected (hint: you're missing a call tochosen_word.upper()
).
â Daniel
May 12 at 10:30
@Sjoerd, I think it's oke to add an excerpt ofsowpods.txt
reduced to a few tens of lines to the question.
â Jan Kuiken
May 12 at 16:15
 |Â
show 1 more comment
2
Your code is not indented properly as shown. Please edit your post, simply copy from your editor, paste in the body of your post, then select all your code and press Ctrl+K / Command+K (Mac).
â Phrancis
May 12 at 5:51
Can you add an excerpt of sowpods.txt? Your code is broken, by the way. Did you test it?
â Daniel
May 12 at 9:25
@Coal_ its a text document on my computer, all it does is generate a random word, the code runs fine. I'm not sure if there is a way i can include it on codereview
â Sjoerd Gerritsen
May 12 at 10:07
The code runs, but it doesn't work as expected (hint: you're missing a call tochosen_word.upper()
).
â Daniel
May 12 at 10:30
@Sjoerd, I think it's oke to add an excerpt ofsowpods.txt
reduced to a few tens of lines to the question.
â Jan Kuiken
May 12 at 16:15
2
2
Your code is not indented properly as shown. Please edit your post, simply copy from your editor, paste in the body of your post, then select all your code and press Ctrl+K / Command+K (Mac).
â Phrancis
May 12 at 5:51
Your code is not indented properly as shown. Please edit your post, simply copy from your editor, paste in the body of your post, then select all your code and press Ctrl+K / Command+K (Mac).
â Phrancis
May 12 at 5:51
Can you add an excerpt of sowpods.txt? Your code is broken, by the way. Did you test it?
â Daniel
May 12 at 9:25
Can you add an excerpt of sowpods.txt? Your code is broken, by the way. Did you test it?
â Daniel
May 12 at 9:25
@Coal_ its a text document on my computer, all it does is generate a random word, the code runs fine. I'm not sure if there is a way i can include it on codereview
â Sjoerd Gerritsen
May 12 at 10:07
@Coal_ its a text document on my computer, all it does is generate a random word, the code runs fine. I'm not sure if there is a way i can include it on codereview
â Sjoerd Gerritsen
May 12 at 10:07
The code runs, but it doesn't work as expected (hint: you're missing a call to
chosen_word.upper()
).â Daniel
May 12 at 10:30
The code runs, but it doesn't work as expected (hint: you're missing a call to
chosen_word.upper()
).â Daniel
May 12 at 10:30
@Sjoerd, I think it's oke to add an excerpt of
sowpods.txt
reduced to a few tens of lines to the question.â Jan Kuiken
May 12 at 16:15
@Sjoerd, I think it's oke to add an excerpt of
sowpods.txt
reduced to a few tens of lines to the question.â Jan Kuiken
May 12 at 16:15
 |Â
show 1 more comment
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
2
Your code is not indented properly as shown. Please edit your post, simply copy from your editor, paste in the body of your post, then select all your code and press Ctrl+K / Command+K (Mac).
â Phrancis
May 12 at 5:51
Can you add an excerpt of sowpods.txt? Your code is broken, by the way. Did you test it?
â Daniel
May 12 at 9:25
@Coal_ its a text document on my computer, all it does is generate a random word, the code runs fine. I'm not sure if there is a way i can include it on codereview
â Sjoerd Gerritsen
May 12 at 10:07
The code runs, but it doesn't work as expected (hint: you're missing a call to
chosen_word.upper()
).â Daniel
May 12 at 10:30
@Sjoerd, I think it's oke to add an excerpt of
sowpods.txt
reduced to a few tens of lines to the question.â Jan Kuiken
May 12 at 16:15