[Solved] Accepting only letters as input [closed]


The answer for the first problem:

You never defined first_name outside of Players_input. This value is just stored inside the function, and get’s deleted afterwards. (more about this in the link added by gjttt1)
There are two ways to solve this:

  • You could make first_name global. But this is a bad style, so I wouldn’t use this option. You would add global first_name at some point in Players_input, before it is written to (so either before or directly after the first print call)
  • You could return first_name, this is the preferred way. Add a return first_name at the end of Players_input, and replace Players_input() with first_name = Players_input().

The answer to the second problem:

Just use this function instead of int(input()) (replace this line with int_input()):

def int_input(prompt="", error_message="You didn't enter an integer!"):
    while True:  # repeat this until the function returns
        inp = input(prompt)  # get the input after autputting the prompt.
        try:  # Try to...
            return int(inp)  # turn it into an integer. If it works, return it.
        except ValueError:  # If it didn't work, it raised a ValueError. In this case...
            if error_message:  # print the error_message if it is not "", false or None.
                print(error_message)

Then you have a third problem: You should just use lowercase letters in function names, to distinguish them from classes. This is just about your style, but it’ll certainly help to develop a good, clear coding style.

I hope I could help,

CodenameLambda

1

solved Accepting only letters as input [closed]