2

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 :)

Community
  • 1
  • 1
  • Are you trying to match the characters of some input against the characters in selection? – M4rtini Mar 04 '14 at 00:14
  • I am trying to get the user input and then check if all the letters entered are matching at least any one of the letters in selection. But I want restrictions, I am struggling with combining them tho (islower, isalpha, isdigit). Maybe I need to ask letters one by one, as I saw trough Claudiu's answer. Still I wonder if there is a way without doing so. – user3375785 Mar 04 '14 at 04:46

7 Answers7

4

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')
Claudiu
  • 224,032
  • 165
  • 485
  • 680
  • He seems to be new. You should probably edit your post to advice that he could avoid the `['a']` , `'a'` conundrum by simply skipping the line where he makes gameword a list. Also that if selection is made a list, it will only be valid for individual(lower case) alphabets and not total words – Guy Mar 04 '14 at 00:05
  • 1
    @Sabyasachi: I suppose. I was going to add that in and then didn't but now I did! – Claudiu Mar 04 '14 at 00:13
  • Anyway I am confused as to why he needs this(for his hangman game). Do you think he might be confusing it to be something that is supposed to check if 'dsasdsasddsa' is a valid word or not? – Guy Mar 04 '14 at 00:15
  • @Sabyasachi: maybe. I wasn't sure which is why I didn't include a solution at first - because, what is the solution exactly? – Claudiu Mar 04 '14 at 00:16
  • thanks for the answers and specifying, that helps a lot! I need it because I want to make it so that someone enters a "gameword" first and then the game uses that word to play. – user3375785 Mar 04 '14 at 00:18
  • Why are we using a `list` for the letters? `import string; if next_letter not in string.ascii_lowercase` will work fine. – Adam Smith Mar 04 '14 at 00:19
  • @adsmith: or `if not next_letter.isalpha()`. i was just keeping as much of the code the same as possible to isolate the issue – Claudiu Mar 04 '14 at 00:28
  • @Claudiu true, and `str.isalpha` is better because it won't yell at the user for uppercase, you can just silently lowercase it. It is nominally slower though. – Adam Smith Mar 04 '14 at 00:32
  • oh, I see that this might be my solution now, asking letters one by one, i'll try it now. I had already edited my OP with what I tried before realizing this but heh :P – user3375785 Mar 04 '14 at 04:36
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

A.J. Uppal
  • 19,117
  • 6
  • 45
  • 76
2

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.

dannymilsom
  • 2,386
  • 1
  • 18
  • 19
  • happy to help - if you find other responses useful in addition to the accepted answer, you can still upvote them using the arrows to the left of each reply. See http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work – dannymilsom Mar 04 '14 at 00:34
  • @user3375785 :D now you do –  Mar 04 '14 at 00:46
2

Try using something like this.

words = 'abcdefghijklmnopqrstuvwxyz'

guess = raw_input('input a gameword> ')
if guess not in words:
    print('not in selection')
  • 1
    Yours is the only answer without a vote, but it's the CORRECT solution to this problem. Let me rectify that.... – Adam Smith Mar 04 '14 at 00:26
1

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')
Lampshady
  • 102
  • 4
1

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.

Guy
  • 604
  • 5
  • 21
  • I didn't know about this method and I don't think I will use it for this game, but I'll use itmaybe in the future. thank you – user3375785 Mar 04 '14 at 00:15
  • @user3375785 Just out of curiosity what are you trying to do with this anyway? I mean how is the question going to fit into your hangman game? – Guy Mar 04 '14 at 00:17
  • @Sabyasachi seems like he's trying to validate the user input when they guess a letter. Ensuring that the letter is, in fact, a letter and not something else. – Adam Smith Mar 04 '14 at 00:25
  • 1
    I need it because I want to make it so that someone enters a "gameword" first and then the game uses that word to play. I wanna make it to a curses game too but, not there yet :P – user3375785 Mar 04 '14 at 00:28
1

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