0

I was given a prompt: Define a function: takes no args, prompts the user for an input. If the user inputs a string with any special characters(?,!,$,etc...), return False. Otherwise, return the input word.

My code currently is:

def hasspec():
     key = input('Give a word: ')
     for q in key:
        if q == "abcdefghijklmnopqrstuvwxyz":
            return key
        else:
            return False

I was expecting to be asked for an input, but instead I am returned: <function hasspec>

Daniel Hao
  • 4,922
  • 3
  • 10
  • 23
Stuff
  • 11
  • 1
    How did you call the function? How did you run the program? – Gino Mempin Jan 03 '23 at 23:33
  • 2
    `q` will be a single character, it will never be equal to `"abcdefghijklmnopqrstuvwxyz"`. – mkrieger1 Jan 03 '23 at 23:34
  • 2
    This problem is caused by failing to **call** the function, but instead only stating its name. This is a common problem for beginners. If not closed as a typo, it should be a duplicate - perhaps of [What does it mean when the parentheses are omitted from a function or method call?](/questions/21785933) - but I am out of close votes today. – Karl Knechtel Jan 03 '23 at 23:35
  • Also the for loop + `return key` only checks for 1 character, specifically only the 1st character. – Gino Mempin Jan 03 '23 at 23:35
  • As for the checking of special characters, possible duplicate of [How to check if a string only contains letters?](https://stackoverflow.com/q/18667410/2745495) – Gino Mempin Jan 03 '23 at 23:36

2 Answers2

1

There are a few issues with your program: first, you should call the function so that the function will come to work for you; secondly the way you check special character is not correct; lastly as earlier comments point out that the q only check one single character.

You could use string lib and puntuation to help you. It includes '!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~'

To correct those issues, this code should achieve your goal:


from string import punctuation

def hasspec():
    word = input('Give a word: ')
     
    if any(c in punctuation for c in word):  # go thru each char and check
        return False
    
    return word                    # it's a good word
            

print(hasspec())                   # try a few words .... now
Daniel Hao
  • 4,922
  • 3
  • 10
  • 23
0

What you need to do in order to make this work is to call the function. You do this by giving it parentheses and any arguments that it needs. In this case it has none so you should just have empty parentheses ().

So you need to do hasspec() in order for the function to run. I recommend watching sentdex's tutorial about functions.

Also note, if what you are trying to do is check that every character is an alphabetical character, then you should instead loop on the key, which contains your input string. This way you get check every character against the string of alphabetical characters.

def hasspec():
     key = input('Give a word: ')
     for c in key:
        if c in "abcdefghijklmnopqrstuvwxyz":
            return key
     return False

There is further improvement is using the .isalpha() method on the string, which automatically checks if all the characters in the string is an alphabetical character. You can do this as such:

def hasspec():
     key = input('Give a word: ')
     return key.isalpha()
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
YousefZ01
  • 315
  • 3
  • 9