#๐Ÿ”’ Print blanks for hangman but fill those that have been guessed

116 messages ยท Page 1 of 1 (latest)

opal mural
#

heres the code

sacred barnBOT
#

@opal mural

Python help channel opened

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.

opal mural
#
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)```
dapper plaza
#

whats wrong

opal mural
#

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

dapper plaza
#

yea yea

opal mural
#

is it my function or smth after while True:

dapper plaza
#

let me try running code by myself

#

user_inputs = []
words_gen(4, 5, 7)

why indent?

opal mural
#

not in my code

dapper plaza
#

give me wordlist file

opal mural
#

probably i did it while editing

candid dragon
#

@opal mural what doe sthe module worldlist do?

dapper plaza
#

yeah

opal mural
#

!paste

sacred barnBOT
#
Pasting large amounts of code

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.

dapper plaza
opal mural
candid dragon
vivid star
#

@opal mural is this solved?

#

or do you still need help

opal mural
vivid star
#

whats ur issue

#

cant be bothered reading everything above this

opal mural
vivid star
#

let me read

dapper plaza
#
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

opal mural
#

oh my

vivid star
opal mural
#

@dapper plaza doesnt fix the main issue

dapper plaza
#

cuz im seeing a different output

#

here's how it is for me

#

really weird

opal mural
#

u didnt read

dapper plaza
#

no ik

opal mural
#

u here?

dapper plaza
#

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"?

opal mural
dapper plaza
#

wha?-

#

dude

#

it'll keep asking for input unless u enter 1

opal mural
#

i added a bug for debugging

dapper plaza
#

only then will it do the necessary step

#

BRO WHAT

#

๐Ÿ˜ญ

opal mural
#

wait uh

#

well its 3 pm rn and i woke up at 2 am today

#

been programming for 13 hours

dapper plaza
#

sleep man sleep

opal mural
#

cant really say woke up i didnt sleep

#

i cant understand stuff rn

dapper plaza
#

yeah take rest

opal mural
dapper plaza
#

wont work

#

i need to go rn to study

#

byeee atb

opal mural
crystal pulsar
#

@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

opal mural
crystal pulsar
# opal mural ```py from wordslist import words import random random.shuffle(words) def words...

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
opal mural
#
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)```
crystal pulsar
#

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

opal mural
#

this is the current code

opal mural
#

im gonna move on to fixing check_win now

crystal pulsar
opal mural
#

and global words used for a test

#

i am gonna fix it now

opal mural
#

here

crystal pulsar
#

oh, your are creating a global from within a function without having defined it in the global scope first ๐Ÿ™ˆ

opal mural
opal mural
crystal pulsar
opal mural
#

i dont understand why using global is such a big deal?

crystal pulsar
# opal mural 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

opal mural
#

assign to main l at bottom

opal mural
# crystal pulsar there are many different reasons not to do it first of it makes the functions ha...
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
crystal pulsar
# opal mural assign to main l at bottom

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

crystal pulsar
opal mural
#

board is done

crystal pulsar
#
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
opal mural
#

ive always done that when working with lists in my past projects too

crystal pulsar
crystal pulsar
opal mural
#

yes

#

missed it

crystal pulsar
#

@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?

sacred barnBOT
#
Python help channel closed with !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.