0

I'm very new to programming, and more new to Python, and I'm having a hard time figuring this out. I'm trying to make a simple text hangman game, and I'm trying to validate player1's input. Here's what I have so far.

a = 0

int = ["1","2","3","4","5","6","7","8","9","0"]

while a == 0:
    print("Please choose any word by typing it here:")
    d = input()
    d = list(d)
    print(d)
    if any(int) == any(d):
        print("Invalid input. Please choose a non-number with no spaces or special characters.")

I can't figure why, whether I include a number in my response or not, it always meets my any(int) == any(d) condition. I know I must just be misunderstanding how any() works.

  • Just in case, `int` is a special word, be careful! – Leau Jul 20 '22 at 17:46
  • 4
    The `any` function returns a boolean. These two `any` calls are not related in any way. In this case, both `any` calls return True, which makes them equal. What you wanted is `if any(k in int for k in d):` – Tim Roberts Jul 20 '22 at 17:48
  • And, by the way, a list of single characters is easier to type as a string: `ints = "1234567890"`. Strings can be iterated just like lists. – Tim Roberts Jul 20 '22 at 17:50
  • 1
    Don't forget to change the value of `a`, or the loop will never terminate. `while True`: with an explicit `break` statement would be cleaner than introducing an otherwise unnecessary flag variable. – chepner Jul 20 '22 at 17:57
  • @TimRoberts And in this case one doesn't even have to define it since [`string.digits`](https://docs.python.org/3/library/string.html#string.digits) can be used. – Matthias Jul 20 '22 at 18:46
  • @zarquon Thanks so much! This makes sense - I've reformatted to – Jonathan Jackson Jul 20 '22 at 19:34

2 Answers2

-1

Try this:

a = 0

ints = ["1","2","3","4","5","6","7","8","9","0"]

while a == 0:
    print("Please choose any word by typing it here:")
    word = input()
    word_list = list(word)
    if any(letter in ints for letter in word_list):
        print("Invalid input. Please choose a non-number with no spaces or special characters.")
Ryan
  • 1,081
  • 6
  • 14
  • 1
    Why? Can you explain? – Leau Jul 20 '22 at 17:51
  • 1
    I wouldn't recommend using a list comprehension here - it is just an unnecessarily complicated version of in for this case. – linger1109 Jul 20 '22 at 17:52
  • 1
    `any` returns a boolean that is `True` if any of the conditions in the parameter passed to it are `True`. The code checks to see if any "letter" in the input is in `ints`. If so, the input is invalid, and the message is printed. – Ryan Jul 20 '22 at 17:52
  • @linger1109 then what would you recommend? – Ryan Jul 20 '22 at 17:53
  • Look at my post -- if you use the `in` keyword, it will check if a value is anywhere within a list, which just does the same thing as here but simpler. – linger1109 Jul 20 '22 at 17:55
  • @linger1109 you need to use a comprehension of some sort. the idea is to check each letter, not just the full word. – Ryan Jul 20 '22 at 17:56
  • Ah -- my fault, I misinterpreted. – linger1109 Jul 20 '22 at 17:58
  • Ha, I made that same misinterpretation in my original answer! – Zarquon Jul 20 '22 at 18:00
-1

You are right: the issue is with how you use the any() function. any() returns either True or False depending on whether or not any of the elements in its iterable argument are True, in other words if they exist and are not False or an empty list or empty string or 0. This will always evaluate as True for int because int does contain an element that is True and the same will happen for d unless you do not give any input when the input() function prompts you (you hit return without typing anything). What your conditional is basically asking is the following:

if True==True

To fix this, just change your code to the following:

a = 0

int = ["1","2","3","4","5","6","7","8","9","0"]

while a == 0:
    print("Please choose any word by typing it here:")
    d = input()
    print(d)
    for letter in d:
        if letter in int:
            print("Invalid input. Please choose a non-number with no spaces or special characters.")
            break

The easiest solution, however, does not involve the int list at all. There is actually a special method in python that can gauge whether or not a string has a number (or special character) in it, and this method, .isalpha() can be used to streamline this process even further. Here's how to implement this solution:

while True:
    d = input("Please choose any word by typing it here: \n")
    print(d)
    if not d.isalpha():
        print("Invalid input. Please choose a non-number with no spaces or special characters.")
Zarquon
  • 146
  • 8