#๐ Print blanks for hangman but fill those that have been guessed
116 messages ยท Page 1 of 1 (latest)
@opal mural
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
from wordslist import words
import random
random.shuffle(words)
def words_gen(no_of_words, min_len, max_len):
global words_used
words_used = []
for word in words:
if min_len <= len(word) <= max_len and word not in words_used:
words_used.append(word)
if len(words_used) == no_of_words:
return words_used
else:
print("Enter a vaild value.")
def display_board(list_name, guessed):
for words in list_name:
print("_"*len(words), end = ' ')
for words in guessed:
print(words)
def get_valid_input(guessed):
while True:
print("Enter what you think it is.")
user_input = input("> ")
if len(user_input) > 1:
print("Enter a single letter. \n")
elif not user_input.isalpha():
print("Enter alphabets. \n")
elif user_input in guessed:
print("Letter already used. \n")
else:
print("You guessed...", user_input)
return user_input
def check_win(word, guessed):
if guessed.sort() == words_used.split().sort():
print("You Won!")
print('''Hangman:
Rules-
1. The computer will think of a random word.
2. You have to guess what the word is.
3. After each wrong guess, a body part is added.
4. If the body is completed, you lose.
5. If you guess what the word is before the body is completed, you win.
''')
user_inputs = []
words_gen(4, 5, 7)
while True:
display_board(words_used, user_inputs)
inputsss = input("> ")
if inputsss =='1':
add_list = get_valid_input(user_inputs)
user_inputs.append(add_list)```
whats wrong
i was debugging my code from the while True thing
oh wait
oh shit
yea
so
i tried entering a b c
to see and try fill in the blanks
and i failed miserably
it did _____ ______ _____ _____ a
___ __ __ __a b
weird underscores but u get it
yea yea
is it my function or smth after while True:
not in my code
give me wordlist file
probably i did it while editing
@opal mural what doe sthe module worldlist do?
yeah
!paste
So that everyone can easily read your code, you can paste it in this website:
https://paste.pythondiscord.com/
After pasting your code, save it by clicking the Paste! button in the bottom left, or by pressing CTRL + S. After doing that, you will be navigated to the new paste's page. Copy the URL and post it here so others can see it.
its a separate file containing list of words for hangman, its not an actual py module
Okay, thats what i was concerned for.
not yet
i aint typing allat
let me read
def get_valid_input(guessed):
while True:
print("Enter what you think it is.")
user_input = input("> ")
if len(user_input) > 1:
print("Enter a single letter. \n")
elif not user_input.isalpha():
print("Enter alphabets. \n")
elif user_input in guessed:
print("Letter already used. \n")
else:
print("You guessed...", user_input)
return user_input
this keeps running in loop, never ends
so this function is never gonna end
and the used alphabets list isnt gonna be updated
I don't understand what you are trying to achieve
@dapper plaza doesnt fix the main issue
no ik
u here?
yeah
u have a lot of problems ur code
inputsss = input("> ")
if inputsss =='1':
add_list = get_valid_input(user_inputs)
user_inputs.append(add_list)
why are u checking where input is "1"?
that was for debugging reasons
i added a bug for debugging
sleep man sleep
yeah take rest
aint sleeping till i get done with this
@opal mural i can agree with @dapper plaza that you have a lot of issues with the code you posted, but i don't agree with his assessment that the get_valid_input function will not end, it will when it hits the return statement if none of the other previous conditions are meet
can u tell me what the issues are? are they only with the 2nd function if yes i fixed it long ago and others later on ill send an update version rq
look at the code you posted here, the indentation of the second line in the below excerpt of your code is indented, which will give a syntax error from python as it doesn't expect any indentation here
user_inputs = []
words_gen(4, 5, 7)
```then, the following function will never work as it is right now
```py
def check_win(word, guessed):
if guessed.sort() == words_used.split().sort():
print("You Won!")
```first of all it's using the wrong variable name `words_used` and you can't use `.sort()` here as it always returns `None`, so you are always comparing `None` == `None` (side note; comparison with `None` should always be done with `is` not `==`) so it will always be evaluate to `True` and thus always print "You win!" when the wrong variable name problem is fixed
i am not using def check win its just there ive not used it yet as i told u many times and i have also stated that everything after user inputs was a test that was wrong
from wordslist import words
import random
random.shuffle(words)
def words_gen(no_of_words, min_len, max_len):
global words_used
words_used = []
for word in words:
if min_len <= len(word) <= max_len and word not in words_used:
words_used.append(word)
if len(words_used) == no_of_words:
return words_used
else:
print("Enter a vaild value.")
def display_board(list_name, guessed):
for word in list_name:
for letter in word:
if letter in guessed:
print(letter, end = '')
else:
print('_', end = '')
print(end=' ')
print()
def get_valid_input(guessed):
print("Enter what you think it is.")
user_input = input("> ")
if len(user_input) > 1:
print("Enter a single letter. \n")
elif not user_input.isalpha():
print("Enter alphabets. \n")
elif user_input in guessed:
print("Letter already used. \n")
else:
print("You guessed...", user_input)
return user_input
def check_win(word, guessed):
if guessed.sort() == words_used.split().sort():
print("You Won!")
print('''Hangman:
Rules-
1. The computer will think of a random word.
2. You have to guess what the word is.
3. After each wrong guess, a body part is added.
4. If the body is completed, you lose.
5. If you guess what the word is before the body is completed, you win.
''')
user_inputs = []
words_gen(4, 5, 7)
while True:
display_board(words_used, user_inputs)
add_list = get_valid_input(user_inputs)
user_inputs.append(add_list)```
you are using the variable words_used both in the global scope and within the scope of the check_win function, none of those has access to a variable with that name, that variable only lives inside the scope of your words_gen function
this is the current code
again. im not using check_win
im gonna move on to fixing check_win now
and still, you are using the words_used variable name here where it doesn't exist
display_board(words_used, user_inputs)
oh, your are creating a global from within a function without having defined it in the global scope first ๐
felt quicker. ngl didnt know what i was doing
and you thought it was bad to update a list from within a function, that is literally nothing compared to this
mb
i dont understand why using global is such a big deal?
there are many different reasons not to do it
first of it makes the functions have side effects that aren't easily understood from calling the function without reading through the whole source code of that functions implementation
it also makes the functions less reusable
and this, when you even instantiate the global from within the function without first declaring it outside of the function, that is really really bad
generally you want a function to return the data to you and then you'll assign the retuned value/data to a variable from the location where you called the function from
ill use a temporary list inside my function then
assign to main l at bottom
from wordslist import words
import random
random.shuffle(words)
def words_gen(no_of_words, min_len, max_len):
temp_list = []
for word in words:
if min_len <= len(word) <= max_len and word not in words_used:
temp_list.append(word)
if len(temp_list) == no_of_words:
return temp_list
else:
print("Not enough words.")
return temp_list
def display_board(list_name, guessed):
for word in list_name:
for letter in word:
if letter in guessed:
print(letter, end = '')
else:
print('_', end = '')
print(end=' ')
print()
def get_valid_input(guessed):
print("Enter what you think it is.")
user_input = input("> ")
if len(user_input) > 1:
print("Enter a single letter. \n")
elif not user_input.isalpha():
print("Enter alphabets. \n")
elif user_input in guessed:
print("Letter already used. \n")
else:
print("You guessed...", user_input)
return user_input
def check_win(word, guessed):
if guessed.sort() == words_used.split().sort():
print("You Won!")
print('''Hangman:
Rules-
1. The computer will think of a random word.
2. You have to guess what the word is.
3. After each wrong guess, a body part is added.
4. If the body is completed, you lose.
5. If you guess what the word is before the body is completed, you win.
''')
words_used = []
user_inputs = []
words_used = words_gen(4, 5, 7)
while True:
display_board(words_used, user_inputs)
add_list = get_valid_input(user_inputs)
user_inputs.append(add_list)``` @crystal pulsar @dapper plaza done
either way, the rest of my points about the implementation of check_win is still valid
additionally you are not using both the variables that are being passed in to that function, which is probably the variables you want to be using instead
IM NOT WORKING ON IT YET
okay okay, so it's the function that prints the word with _'s and the guessed letters in it that you want to be working on right now?
its pretty much done just 1 error
board is done
words_used = words_gen(4, 5, 7)
```was fine as it was, you don't need the
```py
words_used = []
```line above it as that is just being overwritten on the next line
i do that for idk
ive always done that when working with lists in my past projects too
okay, just know that it's totally unnecessary since the empty list is being overwritten by another list on the next line, so it amounts to nothing other then extra work for the computer (also garbage collecting the data that is being thrown away)
def words_gen(no_of_words, min_len, max_len):
temp_list = []
for word in words:
if min_len <= len(word) <= max_len and word not in words_used:
```i'm guessing `words_used` here at the end should be `temp_list`, no?
@opal mural as you have already completed the implementation of your display_board function, was there anything else you wanted to work with in this thread?
no
!close
This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.