Checking each character in a string with regex - python - python

I want to check a string and confirm it only has upper or lowercase alphabetical characters aswell as underscores.
I have this so far but I am getting errors
import re
def addPlayer():
print("\nEnter the players name:")
playerName = input()
for x in range(len(playerName)):
if re.match("[a-zA-Z_]", x):
return
else:
print("Error: Please enter a valid name.")
addPlayer()
Traceback (most recent call last):
File "player.py", line 9, in addPlayer
if re.match("[a-zA-Z_]", x):
File "c:.....Python\Python38-32\lib\re.py", line 189, in match
return _compile(pattern, flags).match(string)
TypeError: expected string or bytes-like object

You can check your entire username at once by checking that all the characters in the string are in the character class [A-Za-z_] i.e.
import re
def addPlayer():
print("\nEnter the players name:")
playerName = input()
if not re.match(r'^[A-Za-z_]+$', playerName):
print("Error: Please enter a valid name.")
addPlayer()

x is number and you passed it to re.match
Your for loop should be like this:
for char in playerName:
if re.match("[a-zA-Z_]", char):
return
else:
print("Error: Please enter a valid name.")
addPlayer()
or event better
regex = re.compile(r'[^a-zA-Z_]')
print(not bool(regex.search("abs123"))) # does not match
print(not bool(regex.search("abs"))) # match

So, the literal cause of that error is that x in an integer. So re.match(pattern, str(x)) fixes the error message. With that said, here is another (perhaps simpler) way to do this:
valid_chars = set([*string.ascii_uppercase, *string.ascii_lowercase, "_"])
for char in string:
if char not in valid_chars:
# Error Condition
## Acceptance Condition

You dont nee
import re
def addPlayer():
playerName='testT_'
m = re.match("[\w_]", playerName)
if m:
print("Success")
else:
print("Error: Please enter a valid name.")

for x in range(len(playerName)):
if re.match("[a-zA-Z_]", x):
return
x's type is int, not string.
you can use
for x in playerName:
if re.match("[a-zA-Z_]", x):
pass
or
for x in playerName:
if not x in string.ascii_letters+'_':
print("Error: Please enter a valid name.")
addPlayer()

Related

How do I check if an input value is a valid string?

So I'm making a game of hangman and it starts with a "BotMaster" entering a string and then how many guesses that player will have to try and guess the string. I only just started and I'm trying to write a function that will check if what the BotMaster put is a valid string. A valid string would be a string that is only letters, no symbols, numbers, or extra spaces. I already have the functions that will remove extra spaces, and non-alpha inputs (So it takes out periods, extra spaces and such) and a function that makes it all lower case, but my function breaks if I enter a number an empty string. How should I add these?
#Imports (this is for later code I haven't written)
import os,time,random
#Removes extra spaces from the function
def space_cull(the_str):
result = the_str
result = result.strip()
result =" ".join(result.split())
the_str = result
return the_str
#Makes the string lowercase
def make_lower(the_str):
the_str = the_str.lower()
return the_str
#Checks if everything in the string are Alpha Inputs
def check_alpha(the_str):
the_str =''.join([char for char in the_str if char.isalnum()])
return the_str
#Ask Botmaster the string they want
def ask_bot():
while True:
bot_str = input('Enter a string for the player to guess: ')
bot_str = space_cull(bot_str)
bot_str = make_lower(bot_str)
bot_str = check_alpha(bot_str)
if bot_str == '':
print('That is not a correct string, try again')
True
return bot_str
ask_bot()
I added the ask_bot() part so I can test the function faster
This is what happens:
Enter a string for the player to guess: 1
#nothing
#Tested again:
Enter a string for the player to guess: ''
That is not a correct string, try again.
#But then exits the loop, which I don't want it to, if the string is wrong I want it to ask them again.
#Tested Again
Enter a string for the player to guess: 'Katze'
#Nothing, which is actually good this time
How do I fix this?
Your while loop will always terminate in the function as it is written.
def ask_bot():
while True:
bot_str = input('Enter a string for the player to guess: ')
bot_str = space_cull(bot_str)
bot_str = make_lower(bot_str)
bot_str = check_alpha(bot_str)
if bot_str == '':
print('That is not a correct string, try again')
True # <- this does nothing
return bot_str # < - this breaks out of the function and the loop
Your code edited to work:
def ask_bot():
while True:
bot_str = input('Enter a string for the player to guess: ')
bot_str = space_cull(bot_str)
bot_str = make_lower(bot_str)
bot_str = check_alpha(bot_str)
if bot_str == '':
print('That is not a correct string, try again')
else: # returns the string if the input is correct
return bot_str # this still breaks out of the function and the loop
# but only if the string has passed the checks
As other answers already mention, you could use str.isalpha() to check that the string is valid, or if you would like to modify the string in place you will need to adjust your check_alpha function like so:
def check_alpha(the_str):
the_str =''.join([char for char in the_str if char.isalpha()])
return the_str
As John Gordon already mentioned the solution is the method "isalpha" of the "str" class.
userInput = input("Your suggestion: ")
if userInput.isalpha():
# do some magic
else:
print("please only type in letters")
You don't need the check_alpha(str) function at all. Modify the ask_bot() as follows.
def ask_bot():
while True:
bot_str = input('Enter a string for the player to guess: ')
bot_str = space_cull(bot_str)
bot_str = make_lower(bot_str)
if not bot_str.isalpha():
if bot_str.isnum():
print("The entered string is a number")
continue
if bot_str == '':
print('That is not a correct string, try again')
continue
continue
break
return bot_str
ask_bot()

How to make a proper name input program in python [duplicate]

This question already has answers here:
Asking the user for input until they give a valid response
(22 answers)
How to validate person names? - Python/Django
(4 answers)
Closed 4 years ago.
I am at the part where I ask the user for their name. So far I got this:
# Import stuff
import time
# Create empty variable
Name = ""
# Ask their name
while Name = ""
Name = input("What is your name? ")
print("")
print(Name)
print("")
time.sleep(3)
So if the user inputs nothing, it repeats the question. But when the user inputs an integer or a float it registers this as a valid name.
How will I be able to make it so that if the Name variable is an integer or a float, it will respond with "Please enter a valid name" and repeat the question?
I'm updating my answer to simplify the code and make it more readable.
The below function is a function that I would use in my own code, I would consider it to be more "proper" than my old answer.
from string import ascii_letters
def get_name():
name = input("What is your name?\n: ").strip().title()
while not all(letter in ascii_letters + " -" for letter in name):
name = input("Please enter a valid name.\n: ").strip().title()
return name
To break this down, the line all(letter in ascii_letters + " -" for letter in name) means "if each letter in name is not an alphabetical character, a space, or a hyphen".
The part letter in ascii_letters + " -" checks to see if a letter is in the string "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ -".
This is repeated by the next part, for letter in name, for every character in the string. This will effectively return a list of booleans, [True, True, True, ...] where any False is a character that did not pass the conditional. Next, this list is passed to the all() function, which returns True if all of the list items are True.
After the all() is executed, conditional is reversed, allowing the loop to continue on the existence of a single failed character.
Old answer is as follows, it will still be useful.
This function should work well for you. Simply check if the string the user entered is alpha characters only, otherwise ask again.
Notice the use of str.isalpha().
def get_name():
name = input("What is your name?\n: ").strip().title()
while not (name.replace("-", "") and
name.replace("-", "").replace(" ", "").isalpha()):
name = input("Please enter a valid name.\n: ").strip().title()
return name
Checking if name will check if the string is empty, and using str.strip() on the values returned will remove any surrounding whitespace (stray spaces) to the left or right of the user input.
The str.replace("-", "") eliminates hyphens while checking validity. Thanks for pointing this out #AGN Gazer.
Now you can just call the function later in your script, or store it for later.
name = get_name().title()
print("You said your name was " + name + ".)
The str.title() converts the letter of each word in a string to uppercase. For example, if I entered my name "jacob birkett", the output (and subsequent value of name would be "Jacob Birkett".
Take a look at the documentation for str.isalpha(), str.strip(), str.replace() and str.title().
You can try this :
while Name == "" or Name.isnumeric() == True:
Name = input("What is your name? ")
print("")
Here if the Name is any numeric value it will ask again, But if the name is like alphanumeric it will accept.
You can use a function like .isalpha() as this will return True if all the string contains all the alphabets:
while True:
Name = input("Please enter a valid name.\n: ")
if name.isalpha()
break
else:
print("Please enter a valid name.")
continue
print(Name)
Or You can try exception handling in python as (but this should be prevented):
try :
int(Name)
print("Please enter a valid name")
...
except:
print("Accepted")
...
This will check if the input is an integer print the error.
You can try:
This will check if variable Name containing numeric data or not.
import time
Name = ""
while Name == "" :
Name = input("What is your name? ")
if not Name.isalpha():
print "It is containing numberic characher or characters"
Name = ""
print("")
print(Name)
print("")
time.sleep(3)
You also can try if name is like "harsha-biyani":
import time
Name = ""
while Name == "" :
Name = input("What is your name? ")
if any(i.isdigit() for i in Name):
print "It is containing numberic characher or characters"
Name = ""
print("")
print(Name)
print("")
time.sleep(3)
You can use:
Name.isalpha()
"3".isalpha()
False
"anna".isalpha()
True

Not getting any results

I am trying to write code to find the length of a string, and to return "invalid entry" when an integer is entered.
def length_of_string(mystring):
if type(mystring) == int:
return "invalid entry"
else:
mystring = input("enter the string ")
return len(mystring)
When I try to execute this function it doesn't give me an error, nor does it produce any solution.
You should move out mystring = input("enter the string ") from else and call the function from main or other place.
def length_of_string(mystring):
if type(mystring) is int:
return "invalid entry"
else:
return len(mystring)
mystring = input("enter the string ")
print(length_of_string(mystring))
If you want the string to be always requested from the user:
def length_of_string():
mystring = input("enter the string ")
try:
int(mystring)
return "invalid entry"
except ValueError:
return len(mystring)
print(length_of_string())
If you want to use the function with a parameter:
def length_of_string(mystring):
try:
int(mystring)
return "invalid entry"
except ValueError:
return len(mystring)
print(length_of_string("a string")) # prints 8
print(length_of_string(1)) # prints invalid entry
print(length_of_string(input("enter the string "))) # prints [input length]
The problem is that you have not called the function. Functions (similar to classes) do not run until you execute them.
Calling them is easy. You just have to call the function name. You also have to pass the necessary parameters to the function (here it is mystring).
length_of_string('Hello World')
To get what is returned you will need to pass it to a variable or print it/perform some other action.
print(length_of_string('Hello World'))
Also if type(mystring) == int: will not work input() is always a string. The thing to do is to test it and see if it can be made into an integer:
try:
int(mystring)
return "invalid entry"
except ValueError:
mystring = input("enter the string ")
return len(mystring)
Entire code:
def length_of_string(mystring):
try:
int(mystring)
return "invalid entry"
except ValueError:
mystring = input("enter the string ")
return len(mystring)
print(length_of_string('Hello World'))
If you pass a string by parameter, it does not make sense to overwrite it again.
I think the solution is:
def length_of_string(mystring):
if (isinstance(mystring, str)):
print "Length of the string: ", len(mystring)
else:
print "Type invalid"

How do I correct my code so that it finds the exceptions and quits accordingly and if its valid it tries the string mutation?

Here is my code:
string = input("Enter a sting")
pos = int(input("Enter the position to be modified"))
try:
b = string.split()
b[pos] = 'k'
b = "".join(b)
print(b)
except:
if string == "":
print("Enter a valid string")
quit()
if pos not in range(len(string)):
print("Enter a valid position to be modified")
quit()
If you try out the code with either a wrong index or an empty string, you will see that the program will run with an IndexError. You can catch that in your except clause. Judging by your question, you said you wanted to quit the program when the exception is caught so the below code will work. I modified the error message in order to not confuse the user as to what they should do next.
string = input("Enter a sting: ")
pos = int(input("Enter the position to be modified: "))
try:
b = string.split()
b[pos] = 'k'
b = "".join(b)
print(b)
except IndexError as e:
if string == "":
print("Error: Need to enter a valid string.")
if pos not in range(len(string)):
print("Error: Need to enter a valid position to be modified.")

How to make the user only enter one character at a time

OK so what I need to do is make my code only allow the user to enter one letter and then one symbol at a time. The example below shows what I want in a better view.
At the moment my code allows the user to enter more than one character at a time which I don't want.
What letter would you like to add? hello
What symbol would you like to pair with hello
The pairing has been added
['A#', 'M*', 'N', 'HELLOhello']
What I want is a message to be displayed like this and the pairing not to be added to the list.
What letter would you like to add? hello
What symbol would you like to pair with hello
You have entered more than one character, the pairing was not added
['A#', 'M*', 'N',].
So far my code for this section is as follows...
It would also be great for when the user enters a number in the letter section, an error message to be printed.
def add_pairing(clues):
addClue = False
letter=input("What letter would you like to add? ").upper()
symbol=input("\nWhat symbol would you like to pair with ")
userInput= letter + symbol
if userInput in clues:
print("The letter either doesn't exist or has already been entered ")
elif len(userInput) ==1:
print("You can only enter one character")
else:
newClue = letter + symbol
addClue = True
if addClue == True:
clues.append(newClue)
print("The pairing has been added")
print (clues)
return clues
The easiest way to ensure user input is with a loop:
while True:
something = raw_input(prompt)
if condition: break
Something set up like this will continue to ask prompt until condition is met. You can make condition anything you want to test for, so for you, it would be len(something) != 1
Your method can be simplified to the following if you let the user enter a letter and symbol pair:
def add_pairing(clues):
pairing = input("Please enter your letter and symbol pairs, separated by a space: ")
clues = pairing.upper().split()
print('Your pairings are: {}'.format(clues))
return clues
Not exactly sure what you want to return but this will check all the entries:
def add_pairing(clues):
addClue = False
while True:
inp = input("Enter a letter followed by a symbol, separated by a space? ").upper().split()
if len(inp) != 2: # make sure we only have two entries
print ("Incorrect amount of characters")
continue
if not inp[0].isalpha() or len(inp[0]) > 1: # must be a letter and have a length of 1
print ("Invalid letter input")
continue
if inp[1].isalpha() or inp[1].isdigit(): # must be anything except a digit of a letter
print ("Invalid character input")
continue
userInput = inp[0] + inp[1] # all good add letter to symbol
if userInput in clues:
print("The letter either doesn't exist or has already been entered ")
else:
newClue = userInput
addClue = True
if addClue:
clues.append(newClue)
print("The pairing has been added")
print (clues)
return clues
I am fan of raising and catching exceptions in similar cases. Might be shocking for people with 'C-ish' background (sloooow), but it is perfectly pythonic and quite readable and flexibile in my opinion.
Also, you should add check for characters outside of set you are expecting:
import string
def read_paring():
letters = string.ascii_uppercase
symbols = '*##$%^&*' # whatever you want to allow
letter = input("What letter would you like to add? ").upper()
if (len(letter) != 1) or (letter not in letters):
raise ValueError("Only a single letter is allowed")
msg = "What symbol would you like to pair with '{}'? ".format(letter)
symbol = input(msg).upper()
if (len(symbol) != 1) or (symbol not in symbols):
raise ValueError("Only one of '{}' is allowed".format(symbols))
return (letter, symbol)
def add_pairing(clues):
while True:
try:
letter, symbol = read_paring()
new_clue = letter + symbol
if new_clue in clues:
raise ValueError("This pairing already exists")
break # everything is ok
except ValueError as err:
print(err.message)
print("Try again:")
continue
# do whatever you want with letter and symbol
clues.append(new_clue)
print(new_clue)
return clues

Categories

Resources