-3

I'm making a basic text-based hangman game, and I'm stumped as to why inpLetter is returning None from letterVerify()? It's expected to return a string response so I'm unsure why it isn't.

def letterVerify(prompt):
    try:
        inp = input(prompt)
        verify = str.count(inp)
        if (verify > 1) or (verify < 1):
            print("Please enter one character.")

        if inp not in alphabet or alphabetCaps:
            print("Please enter a letter from the English alphabet.")

        else:
            return inp

    except:
        print("Please enter a letter from the English alphabet.")
inpLetter = letterVerify("Enter a letter you think is in this word. ")
ray
  • 1
  • 1
  • Welcome to stack overflow! Please provide a [mcve] in the text of your question, not as a picture or external link – G. Anderson Feb 12 '20 at 22:43
  • Welcome to StackOverflow. [On topic](https://stackoverflow.com/help/on-topic), [how to ask](https://stackoverflow.com/help/how-to-ask), and ... [the perfect question](https://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/) apply here. – Prune Feb 12 '20 at 22:46
  • [Test multiple values](https://stackoverflow.com/questions/15112125/how-to-test-multiple-variables-against-a-value) – Prune Feb 12 '20 at 22:53
  • 1
    [Loop until valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – Prune Feb 12 '20 at 22:54
  • See this lovely [debug](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) blog for help. Insert useful output statements to trace the control and data flow. Remove inapplicable code; reduce working code to a hard-coded result. As the posting guidelines say, "make it easy for others to help you." Your code has two known problems that I've linked to above, and you've failed to supply us with any debugging attempts. – Prune Feb 12 '20 at 22:56
  • 1
    Does this answer your question? [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – Gino Mempin Feb 13 '20 at 07:21

2 Answers2

0

The problem is in line 4, verify = str.count(inp). count() function returns the number of times a particular character has appeared in a string. It is used as

string = 'abcabcabcabcabc'
string.count('a')

This returns 5. Your code could not find this error because of the try-except block. Whenever this error was raised, it was caught by the exception block, and hence made it difficult to debug. You can rather try using the len() function. This will do the same work you want. It will check if the length of input is 1 or not. Modifications to your code are as

def letterVerify(prompt):
    inp = input(prompt)
    verify = len(inp)
    if verify > 1 or verify < 1:
        print('Please enter 1 character')
    elif not inp.isalpha():
        print('Please enter a letter from English alphabet')
    else:
        return inp
inpLetter = letterVerify('Enter a letter you think is in this word')

Also, you did not specify what alphabet and alphabetCaps are. I have used isalpha() method, that checks only for alphabetical characters (what you needed). Hope this resolves your issue.

Swati Srivastava
  • 1,102
  • 1
  • 12
  • 18
0

I made your function a little terse for better readability.

  • First, you should avoid naming your function in camelCase, this violates PEP8. Rather you should use snake_case while naming functions and variables.

  • Also, simple length checking will do the verify trick. str.count returns the number of times a letter appears in a string, you don't want that here.

  • You don't need the exception handler here. Python will internally make your inputs str type.

  • You can lower your string and make comparison with the built in string.ascii_lowercase property. This will eliminate redundant control flow logic.

Here is the function, refactored:

def letter_verify(prompt):

    inp = input(prompt)

    # remove extra whitespace
    inp = inp.strip()

    # simple length checking with len function will do the trick
    if len(inp) != 1:
        print("Please enter one character.")

    # just make your input to lowercase and then compare
    # this will help you avoid comparison with two cases
    elif inp.lower() not in string.ascii_lowercase:
        print("Please enter a letter from the English alphabet.")

    else:
        return inp
Redowan Delowar
  • 1,580
  • 1
  • 14
  • 36