How to store user inputted strings in a variable and then see if those inputs are contained in a word
I'm trying to make a hangman game. At the moment I'm still trying to figure out how to store users inputted strings in a variable.
I need this because if a user guesses the word before the ten guesses it keeps asking them for another letter.
I've tried creating a variable and then updating the variable as the user entered more letters. However that doesn't work in cases the user doesn't enter all the letters in the correct order the program is obviously not going to recognize this as the correct word.
Basically I need a way to store the inputted strings (one letter at a time) and to be able to check if all of those letters are contained in the five letter word.
import random
print("Welcome to hangman, guess the five letter word")
words =["china", "ducks", "glass"]
correct_word = (random.choice(words))
trials = 10
for trial in range(trials):
guess = input(str("Enter Character:"))
if (len(guess) > 1):
print("You are not allowed to enter more than one character at a time")
continue
if guess in correct_word:
print("Well done " + guess + " is in the list!")
else:
print("Sorry " + guess + " is not included")
python
add a comment |
I'm trying to make a hangman game. At the moment I'm still trying to figure out how to store users inputted strings in a variable.
I need this because if a user guesses the word before the ten guesses it keeps asking them for another letter.
I've tried creating a variable and then updating the variable as the user entered more letters. However that doesn't work in cases the user doesn't enter all the letters in the correct order the program is obviously not going to recognize this as the correct word.
Basically I need a way to store the inputted strings (one letter at a time) and to be able to check if all of those letters are contained in the five letter word.
import random
print("Welcome to hangman, guess the five letter word")
words =["china", "ducks", "glass"]
correct_word = (random.choice(words))
trials = 10
for trial in range(trials):
guess = input(str("Enter Character:"))
if (len(guess) > 1):
print("You are not allowed to enter more than one character at a time")
continue
if guess in correct_word:
print("Well done " + guess + " is in the list!")
else:
print("Sorry " + guess + " is not included")
python
add a comment |
I'm trying to make a hangman game. At the moment I'm still trying to figure out how to store users inputted strings in a variable.
I need this because if a user guesses the word before the ten guesses it keeps asking them for another letter.
I've tried creating a variable and then updating the variable as the user entered more letters. However that doesn't work in cases the user doesn't enter all the letters in the correct order the program is obviously not going to recognize this as the correct word.
Basically I need a way to store the inputted strings (one letter at a time) and to be able to check if all of those letters are contained in the five letter word.
import random
print("Welcome to hangman, guess the five letter word")
words =["china", "ducks", "glass"]
correct_word = (random.choice(words))
trials = 10
for trial in range(trials):
guess = input(str("Enter Character:"))
if (len(guess) > 1):
print("You are not allowed to enter more than one character at a time")
continue
if guess in correct_word:
print("Well done " + guess + " is in the list!")
else:
print("Sorry " + guess + " is not included")
python
I'm trying to make a hangman game. At the moment I'm still trying to figure out how to store users inputted strings in a variable.
I need this because if a user guesses the word before the ten guesses it keeps asking them for another letter.
I've tried creating a variable and then updating the variable as the user entered more letters. However that doesn't work in cases the user doesn't enter all the letters in the correct order the program is obviously not going to recognize this as the correct word.
Basically I need a way to store the inputted strings (one letter at a time) and to be able to check if all of those letters are contained in the five letter word.
import random
print("Welcome to hangman, guess the five letter word")
words =["china", "ducks", "glass"]
correct_word = (random.choice(words))
trials = 10
for trial in range(trials):
guess = input(str("Enter Character:"))
if (len(guess) > 1):
print("You are not allowed to enter more than one character at a time")
continue
if guess in correct_word:
print("Well done " + guess + " is in the list!")
else:
print("Sorry " + guess + " is not included")
python
python
edited Dec 28 '18 at 17:14
Oleg
1438
1438
asked Dec 28 '18 at 16:31
user10830595
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
With the help of an auxiliar list, this little guy will do the trick (I put some comments to better explain what I did):
import random
import sys
print("Welcome to hangman, guess the five letter word")
words =["china", "ducks", "glass"]
correct_word = (random.choice(words))
trials = 10
trial = 0
#the list starts empty, since the user didn't guessed any letter
guessed_letters =
while(trial < trials):
guess = input(str("Enter Charcter:"))
if (len(guess) > 1):
print("You are not allowed to enter more than one charcter at a time")
continue
if (guess in correct_word) and (guess not in guessed_letters):
#once our user guesses a letter that he didn't guessed before, it will be added to our list of guessed letters
print("Well done " + guess + " is in the list!")
guessed_letters += guess
#in here we compare the size of the list with the size of the correct_word without duplicates
#if it's the same size, boom: the user won!
if (len(guessed_letters) == len(set(correct_word))):
print("Congratulations! You won!")
sys.exit()
else:
#just to avoid confusing the user with duplicate letters
if (guess in guessed_letters):
print("You already tried letter " + guess + "! Try another letter")
trial -= 1
else:
print("Sorry " + guess + " is not included")
print("Remaining chances: " + str(trials - trial))
trial += 1
print("nnnGame over!!!!")
1
Thanks this is really helpful!
– user10830595
Dec 28 '18 at 17:51
I'm a little confused on this line right here: len(set(correct_word))): specifically what is the "set" doing?
– user10830595
Dec 28 '18 at 18:16
I guess this shall help you -> docs.python.org/3/tutorial/datastructures.html#sets
– Helena Martins
Dec 28 '18 at 18:19
Could I just change words = {"china", "ducks", "glass"}. Would that work and then could I get rid of the (set) command. Just trying to understand it better.
– user10830595
Dec 28 '18 at 18:22
Theset
assures the program won't consider duplicates of the correct_word. For example, if the work isglass
and user tries the letter "s", the program would add the letter once to theguessed_letters
list. But there are two s ocurrences in the wordglass
! To solve this situation, I used theset
to consider each letter from thecorrect_word
just once. Then, when we compare sizes, there won't be any duplicate issue. For example, in theglass
situation, the setting of glass would only contain letters "g", "l", "a" and "s"!
– Helena Martins
Dec 28 '18 at 18:30
|
show 3 more comments
It seems like a set is what you need.
You start with an empty set()
and add the letter every time. To check if the letters are enough, use saved_set == set(correct_word)
.
add a comment |
All you need is to replace:
guess = input(str("Enter Charcter:"))
by:
guess = str(sys.stdin.readline().rstrip('n'))
add a comment |
Your Answer
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: "1"
};
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',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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
});
}
});
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%2fstackoverflow.com%2fquestions%2f53961529%2fhow-to-store-user-inputted-strings-in-a-variable-and-then-see-if-those-inputs-ar%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
With the help of an auxiliar list, this little guy will do the trick (I put some comments to better explain what I did):
import random
import sys
print("Welcome to hangman, guess the five letter word")
words =["china", "ducks", "glass"]
correct_word = (random.choice(words))
trials = 10
trial = 0
#the list starts empty, since the user didn't guessed any letter
guessed_letters =
while(trial < trials):
guess = input(str("Enter Charcter:"))
if (len(guess) > 1):
print("You are not allowed to enter more than one charcter at a time")
continue
if (guess in correct_word) and (guess not in guessed_letters):
#once our user guesses a letter that he didn't guessed before, it will be added to our list of guessed letters
print("Well done " + guess + " is in the list!")
guessed_letters += guess
#in here we compare the size of the list with the size of the correct_word without duplicates
#if it's the same size, boom: the user won!
if (len(guessed_letters) == len(set(correct_word))):
print("Congratulations! You won!")
sys.exit()
else:
#just to avoid confusing the user with duplicate letters
if (guess in guessed_letters):
print("You already tried letter " + guess + "! Try another letter")
trial -= 1
else:
print("Sorry " + guess + " is not included")
print("Remaining chances: " + str(trials - trial))
trial += 1
print("nnnGame over!!!!")
1
Thanks this is really helpful!
– user10830595
Dec 28 '18 at 17:51
I'm a little confused on this line right here: len(set(correct_word))): specifically what is the "set" doing?
– user10830595
Dec 28 '18 at 18:16
I guess this shall help you -> docs.python.org/3/tutorial/datastructures.html#sets
– Helena Martins
Dec 28 '18 at 18:19
Could I just change words = {"china", "ducks", "glass"}. Would that work and then could I get rid of the (set) command. Just trying to understand it better.
– user10830595
Dec 28 '18 at 18:22
Theset
assures the program won't consider duplicates of the correct_word. For example, if the work isglass
and user tries the letter "s", the program would add the letter once to theguessed_letters
list. But there are two s ocurrences in the wordglass
! To solve this situation, I used theset
to consider each letter from thecorrect_word
just once. Then, when we compare sizes, there won't be any duplicate issue. For example, in theglass
situation, the setting of glass would only contain letters "g", "l", "a" and "s"!
– Helena Martins
Dec 28 '18 at 18:30
|
show 3 more comments
With the help of an auxiliar list, this little guy will do the trick (I put some comments to better explain what I did):
import random
import sys
print("Welcome to hangman, guess the five letter word")
words =["china", "ducks", "glass"]
correct_word = (random.choice(words))
trials = 10
trial = 0
#the list starts empty, since the user didn't guessed any letter
guessed_letters =
while(trial < trials):
guess = input(str("Enter Charcter:"))
if (len(guess) > 1):
print("You are not allowed to enter more than one charcter at a time")
continue
if (guess in correct_word) and (guess not in guessed_letters):
#once our user guesses a letter that he didn't guessed before, it will be added to our list of guessed letters
print("Well done " + guess + " is in the list!")
guessed_letters += guess
#in here we compare the size of the list with the size of the correct_word without duplicates
#if it's the same size, boom: the user won!
if (len(guessed_letters) == len(set(correct_word))):
print("Congratulations! You won!")
sys.exit()
else:
#just to avoid confusing the user with duplicate letters
if (guess in guessed_letters):
print("You already tried letter " + guess + "! Try another letter")
trial -= 1
else:
print("Sorry " + guess + " is not included")
print("Remaining chances: " + str(trials - trial))
trial += 1
print("nnnGame over!!!!")
1
Thanks this is really helpful!
– user10830595
Dec 28 '18 at 17:51
I'm a little confused on this line right here: len(set(correct_word))): specifically what is the "set" doing?
– user10830595
Dec 28 '18 at 18:16
I guess this shall help you -> docs.python.org/3/tutorial/datastructures.html#sets
– Helena Martins
Dec 28 '18 at 18:19
Could I just change words = {"china", "ducks", "glass"}. Would that work and then could I get rid of the (set) command. Just trying to understand it better.
– user10830595
Dec 28 '18 at 18:22
Theset
assures the program won't consider duplicates of the correct_word. For example, if the work isglass
and user tries the letter "s", the program would add the letter once to theguessed_letters
list. But there are two s ocurrences in the wordglass
! To solve this situation, I used theset
to consider each letter from thecorrect_word
just once. Then, when we compare sizes, there won't be any duplicate issue. For example, in theglass
situation, the setting of glass would only contain letters "g", "l", "a" and "s"!
– Helena Martins
Dec 28 '18 at 18:30
|
show 3 more comments
With the help of an auxiliar list, this little guy will do the trick (I put some comments to better explain what I did):
import random
import sys
print("Welcome to hangman, guess the five letter word")
words =["china", "ducks", "glass"]
correct_word = (random.choice(words))
trials = 10
trial = 0
#the list starts empty, since the user didn't guessed any letter
guessed_letters =
while(trial < trials):
guess = input(str("Enter Charcter:"))
if (len(guess) > 1):
print("You are not allowed to enter more than one charcter at a time")
continue
if (guess in correct_word) and (guess not in guessed_letters):
#once our user guesses a letter that he didn't guessed before, it will be added to our list of guessed letters
print("Well done " + guess + " is in the list!")
guessed_letters += guess
#in here we compare the size of the list with the size of the correct_word without duplicates
#if it's the same size, boom: the user won!
if (len(guessed_letters) == len(set(correct_word))):
print("Congratulations! You won!")
sys.exit()
else:
#just to avoid confusing the user with duplicate letters
if (guess in guessed_letters):
print("You already tried letter " + guess + "! Try another letter")
trial -= 1
else:
print("Sorry " + guess + " is not included")
print("Remaining chances: " + str(trials - trial))
trial += 1
print("nnnGame over!!!!")
With the help of an auxiliar list, this little guy will do the trick (I put some comments to better explain what I did):
import random
import sys
print("Welcome to hangman, guess the five letter word")
words =["china", "ducks", "glass"]
correct_word = (random.choice(words))
trials = 10
trial = 0
#the list starts empty, since the user didn't guessed any letter
guessed_letters =
while(trial < trials):
guess = input(str("Enter Charcter:"))
if (len(guess) > 1):
print("You are not allowed to enter more than one charcter at a time")
continue
if (guess in correct_word) and (guess not in guessed_letters):
#once our user guesses a letter that he didn't guessed before, it will be added to our list of guessed letters
print("Well done " + guess + " is in the list!")
guessed_letters += guess
#in here we compare the size of the list with the size of the correct_word without duplicates
#if it's the same size, boom: the user won!
if (len(guessed_letters) == len(set(correct_word))):
print("Congratulations! You won!")
sys.exit()
else:
#just to avoid confusing the user with duplicate letters
if (guess in guessed_letters):
print("You already tried letter " + guess + "! Try another letter")
trial -= 1
else:
print("Sorry " + guess + " is not included")
print("Remaining chances: " + str(trials - trial))
trial += 1
print("nnnGame over!!!!")
edited Dec 28 '18 at 18:45
answered Dec 28 '18 at 16:42
Helena MartinsHelena Martins
620117
620117
1
Thanks this is really helpful!
– user10830595
Dec 28 '18 at 17:51
I'm a little confused on this line right here: len(set(correct_word))): specifically what is the "set" doing?
– user10830595
Dec 28 '18 at 18:16
I guess this shall help you -> docs.python.org/3/tutorial/datastructures.html#sets
– Helena Martins
Dec 28 '18 at 18:19
Could I just change words = {"china", "ducks", "glass"}. Would that work and then could I get rid of the (set) command. Just trying to understand it better.
– user10830595
Dec 28 '18 at 18:22
Theset
assures the program won't consider duplicates of the correct_word. For example, if the work isglass
and user tries the letter "s", the program would add the letter once to theguessed_letters
list. But there are two s ocurrences in the wordglass
! To solve this situation, I used theset
to consider each letter from thecorrect_word
just once. Then, when we compare sizes, there won't be any duplicate issue. For example, in theglass
situation, the setting of glass would only contain letters "g", "l", "a" and "s"!
– Helena Martins
Dec 28 '18 at 18:30
|
show 3 more comments
1
Thanks this is really helpful!
– user10830595
Dec 28 '18 at 17:51
I'm a little confused on this line right here: len(set(correct_word))): specifically what is the "set" doing?
– user10830595
Dec 28 '18 at 18:16
I guess this shall help you -> docs.python.org/3/tutorial/datastructures.html#sets
– Helena Martins
Dec 28 '18 at 18:19
Could I just change words = {"china", "ducks", "glass"}. Would that work and then could I get rid of the (set) command. Just trying to understand it better.
– user10830595
Dec 28 '18 at 18:22
Theset
assures the program won't consider duplicates of the correct_word. For example, if the work isglass
and user tries the letter "s", the program would add the letter once to theguessed_letters
list. But there are two s ocurrences in the wordglass
! To solve this situation, I used theset
to consider each letter from thecorrect_word
just once. Then, when we compare sizes, there won't be any duplicate issue. For example, in theglass
situation, the setting of glass would only contain letters "g", "l", "a" and "s"!
– Helena Martins
Dec 28 '18 at 18:30
1
1
Thanks this is really helpful!
– user10830595
Dec 28 '18 at 17:51
Thanks this is really helpful!
– user10830595
Dec 28 '18 at 17:51
I'm a little confused on this line right here: len(set(correct_word))): specifically what is the "set" doing?
– user10830595
Dec 28 '18 at 18:16
I'm a little confused on this line right here: len(set(correct_word))): specifically what is the "set" doing?
– user10830595
Dec 28 '18 at 18:16
I guess this shall help you -> docs.python.org/3/tutorial/datastructures.html#sets
– Helena Martins
Dec 28 '18 at 18:19
I guess this shall help you -> docs.python.org/3/tutorial/datastructures.html#sets
– Helena Martins
Dec 28 '18 at 18:19
Could I just change words = {"china", "ducks", "glass"}. Would that work and then could I get rid of the (set) command. Just trying to understand it better.
– user10830595
Dec 28 '18 at 18:22
Could I just change words = {"china", "ducks", "glass"}. Would that work and then could I get rid of the (set) command. Just trying to understand it better.
– user10830595
Dec 28 '18 at 18:22
The
set
assures the program won't consider duplicates of the correct_word. For example, if the work is glass
and user tries the letter "s", the program would add the letter once to the guessed_letters
list. But there are two s ocurrences in the word glass
! To solve this situation, I used the set
to consider each letter from the correct_word
just once. Then, when we compare sizes, there won't be any duplicate issue. For example, in the glass
situation, the setting of glass would only contain letters "g", "l", "a" and "s"!– Helena Martins
Dec 28 '18 at 18:30
The
set
assures the program won't consider duplicates of the correct_word. For example, if the work is glass
and user tries the letter "s", the program would add the letter once to the guessed_letters
list. But there are two s ocurrences in the word glass
! To solve this situation, I used the set
to consider each letter from the correct_word
just once. Then, when we compare sizes, there won't be any duplicate issue. For example, in the glass
situation, the setting of glass would only contain letters "g", "l", "a" and "s"!– Helena Martins
Dec 28 '18 at 18:30
|
show 3 more comments
It seems like a set is what you need.
You start with an empty set()
and add the letter every time. To check if the letters are enough, use saved_set == set(correct_word)
.
add a comment |
It seems like a set is what you need.
You start with an empty set()
and add the letter every time. To check if the letters are enough, use saved_set == set(correct_word)
.
add a comment |
It seems like a set is what you need.
You start with an empty set()
and add the letter every time. To check if the letters are enough, use saved_set == set(correct_word)
.
It seems like a set is what you need.
You start with an empty set()
and add the letter every time. To check if the letters are enough, use saved_set == set(correct_word)
.
edited Dec 28 '18 at 16:59
Oleg
1438
1438
answered Dec 28 '18 at 16:35
iBugiBug
19k53362
19k53362
add a comment |
add a comment |
All you need is to replace:
guess = input(str("Enter Charcter:"))
by:
guess = str(sys.stdin.readline().rstrip('n'))
add a comment |
All you need is to replace:
guess = input(str("Enter Charcter:"))
by:
guess = str(sys.stdin.readline().rstrip('n'))
add a comment |
All you need is to replace:
guess = input(str("Enter Charcter:"))
by:
guess = str(sys.stdin.readline().rstrip('n'))
All you need is to replace:
guess = input(str("Enter Charcter:"))
by:
guess = str(sys.stdin.readline().rstrip('n'))
answered Dec 28 '18 at 16:47
Walid Da.Walid Da.
5891413
5891413
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- 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%2fstackoverflow.com%2fquestions%2f53961529%2fhow-to-store-user-inputted-strings-in-a-variable-and-then-see-if-those-inputs-ar%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