Question

I know this post here : Testing user input against a list in python

but, being new (here and at programming) I was not sure if bumping a 3 years old post was ok or if I needed to make a new one. I apologize for that.

I checked the documentation also but I did not understand fully after reading, why this code would NOT print the line "not in selection" after running it.

I would like to make it so that the user input is tested against the list "selection". (assuming the user enters only low caps between a to z)

I tried inputs like 'a' or 'test' but "not in selection" still prints out, as if it didn't recognize the input.

Could someone help me with this one ? I am trying to make a hangman game :)

selection = list('abcdefghijklmnopqrstuvwxyz')
gameword = []

user_input = input('input a gameword> ') 
gameword = list(user_input) 
if gameword not in selection:
    print('not in selection')

Edit++

I realized that I need all of those: "islower, isalpha, isdigit

problem is, I cant seem to combine all that and still make it work. or come up with a more appropriated way to do this.

this is what I ended up with before I realize I needed to combine them all:

while True:                                      # loop to get the user input
user_input = input('input> ')                    # store input in 'user_input'
gameword = list(user_input)                      # list inside gameword
if user_input == 'q':                            # loop ends when q is entered
    break                                        # break statement
if len(user_input) <= 4 or len(user_input) >= 9: # checks the length of the string input 
    print('5 to 8 letters ')                     # prints the restriction on screen
for i in gameword:                               #
    if i.islower() not in selection              # checks if lowercase are used
        print('only low letters please')         # prints the restriction on screen
for i in gameword:                               #
    if i.isalpha() not in selection:             # check if is alphabet letter are used
        print('only letters')                    # prints the restriction onscreen
for i in gameword:                               #
    if i.isdigit() in selection:                 # checks if digits are in word list
        print('no numbers plz')                  #

`

but it keeps printing the lines "only low letters please" instead of just check conditions and then print "wrong input message" then tries to get the input again before the game starts...

I would love more advice about this :)

Was it helpful?

Solution 2

Well say gameword was hello. Now if the program searches for the list ['h', 'e', 'l', 'l', 'o'] in abcdefghijklmnopqrstuvwxyz, it isn't going to find it, because the entire word isn't present as a whole. You might want to change your code as such:

selection = list('abcdefghijklmnopqrstuvwxyz')
gameword = []

user_input = input('input a gameword> ') 
gameword = list(user_input) 
for k in gameword:
    if k.lower() not in selection:
        print('not in selection') 

You are checking if the entire list is in the selection, which it isn't. A fix for this is using a for loop to loop over each individual character in selection, not every character at once.

EDIT: Here is the second set of code, answering your second question:

selection = list('abcdefghijklmnopqrstuvwxyz')
gameword = []

user_input = input('input a gameword> ')
user_input = str(user_input)
gameword = list(user_input)
for k in gameword:
    if k.isdigit() == True:
        print('no digits please')
        break
    if k.isalpha() == False:
        print('only alphabets please')
        break
    if k.islower() == False:
        print('only lowercase please')
        break
    if k not in selection:
        print('not in selection')
        break

I hope I understood what you were asking correctly :D

OTHER TIPS

selection contains a list of characters: ['a', 'b', ..., 'z'].

gameword is a list of whatever the user typed in. If the user typed in a, then gameword is ['a'].

Then you ask whether gameword is in selection, that is, whether selection contains ['a']. It doesn't, though, it contains 'a', the character, not ['a'], the list with one element in it.

I'm guessing you want the user to just type in the next letter to guess? Try this:

selection = list('abcdefghijklmnopqrstuvwxyz')
next_letter = input('input a letter> ') 
if len(next_letter) != 1:
    print('Not a letter')
elif next_letter not in selection:
    print('not in selection')

Try using something like this.

words = 'abcdefghijklmnopqrstuvwxyz'

guess = raw_input('input a gameword> ')
if guess not in words:
    print('not in selection')

It sounds like you want to validate that the user_input string only contains alphabetic characters (a-z). For this you can use the isalpha() string method.

if not gameword.isalpha():
    print("Not all alphabetical characters")

To check that the string is all lower case, you can also use the string.islower() method.

if not gameword.islower():
    print("Not all lower case characters")

To convert a string to all lowercase, you can use the string.lower() method.

It appears you are searching for a string in a one item list. For this type of game, I would utilize a tuple since each input is unique.

selection = ('a','b','c','d')
gameword = []
....... //input game words
for word in gameword:
    if word not in selection:
        print('not in selection')

You say you are building a hangman game, so do you want to check if userinput is a valid English word? If so, then this will not work. You need to download a wordlist. There are plenty of open source resources available.

Save the text file as 'words.txt' in your home folder. Then, you can do this,

>>>s=set(i for i in open('words.txt'))
>>>userinput=input('Enter your word')
>>>if userinput not in s:
        print('Not a valid word')

Here's one link you can try.

Ask for input, and then use a bunch of if...else statements to check for islower(), isalpha(), and so on

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top