Not sure how to accomplish what I want:
I am first asking the user to input a letter ranging from A to H. Once I grab that input, I want do STUFF if the input was valid, but I'm not sure how to verify that the input was valid.
while True:
input= raw_input("Enter letter between A-H: ")
if input "BETWEEN A-H": # (this is where I'm stuck)
# DO STUFF
break
else:
print "Invalid input. Try again"
continue
You can get the Unicode value of the character (with ord(character)) and check if it is in the desired range.
Related
I've been working on a project and I've to check if the strings entered by user are alphanumeric or not.
For now, I've built my code and there's one function which needs to check if any word is alphanumeric or not.
The program is to let user enter a sentence along with his license number which would be alphanumeric like '221XBCS'. So, if the user enter suppose- 'My license number is 221124521' instead of 221XBCS I want the program to stop.
But my current program is assuming the re.match condition is true always. WHY IS IT SO??
import re
s = input("Please enter here:")
if re.search(r'\bnumber \b',s):
x = (s.split('number ')[1])
y = x.split()
z = y[0]
print(z)
if re.match('^[\w-]+$', z):
print('true')
else:
print('False')
The output looks like this for now:
Please enter here:my license number is 221
is
true
I want my program to grab alnum value from the input. That's all!
I think I understood your condition: the user should enter his license number which should contain only alphabetic characters AND numbers (both):
With built-in functions:
s = input("Please enter here:")
l_numbers = list(filter(lambda w: not w.isdigit() and not w.isalpha() and w.isalnum(), s.strip().split()))
l_number = l_numbers[0] if l_numbers else ''
print(l_number)
Let's say the user entered My license number is 221XBCS thanks.
The output will be:
221XBCS
With regular expressions, it is often valuable to look at the problem the reverse way.
It is, in this case, better to look if the string is NOT a pure numerical than looking if it IS an alphanumerical.
if re.match('[^\d]', z):
print('The string is no a pure numerical')
I'm an absolute beginner trying to learn string validation. I have a variable about to store user input:
Text_input = raw_input('Type anything: ')
I want to check if Text_input contains at least one alphanumeric character. (If not, the program should print a message such as "Try again!" and ask the user to type again.) So, typing "A#" should pass but "#" should not. Any suggestions?
This worked for me:
Text_input = raw_input('Type anything: ')
if any(char.isalpha() or char.isdigit() for char in Text_input):
print "Input contains at least one alphanumeric character."
else:
print "Input must contain at least one alphanumeric character."
I'm pretty new to programming in python, so this is giving me problems. I'm searching for a way to uppercase a letter of a user's choosing in a word of user's choosing. For example, user writes staircase and as character he writes a and the end result would be stAircAse.
I know the code to ask user a question
promt='enter word:\n'
originalString=raw_input(promt)
promt='enter a character:\n'
upperCharacter=raw_input(promt)
But other then that I'm lost and would appreciate help.
i tried this code, but its not the user input of a letter to upper case
def chartoupper(instring,inchar):
indexes=[3,7]
chars=list(originalString)
for i in indexes:
chars[i]=chars[i].upper()
string=''.join(chars)
return string
promt='enter word:\n'
originalString=raw_input(promt)
promt='enter a character:\n'
upperCharacter=raw_input(promt)
newstring=chartoupper(originaString,upperCharacter)
print originaString
print upperCharacter
print newstring
print inchar
Try this -
if(len(upperCharacter) == 1):
finalString = originalString.replace(upperCharacter, upperCharacter.upper())
print finalString
As you should check if the user has indeed just added a single character, we added the if condition.
We are replacing all the occurrences of the character upperCharacter in originalString and assigning it to finalString. Refer the docs for this method
Answer for edited question --
def chartoupper(instring, inchar):
string = instring.replace(inchar, inchar.upper())
return string
promt='enter word:\n'
originalString=raw_input(promt)
promt='enter a character (note that only the 1st character will be used):\n'
upperCharacter=raw_input(promt)[0]
newstring=chartoupper(originalString, upperCharacter)
print originalString
print upperCharacter
print newstring
How can I let user enter in a value right after a string? For example, I want the user to be able to enter in
a value after this string on the same line "Please enter in the length of the first side: ". However, the
raw_input() function starts at a new line. How can I continue on the same line?
raw_input takes an argument. So just do
raw_input('Please enter in the length of the first side: ')
and the user will be prompted to input right after the colon.
I'm trying to find any occurrences of a character repeating more than 2 times in a user entered string. I have this, but it doesn't go into the if statement.
password = asDFwe23df333
s = re.compile('((\w)\2{2,})')
m = s.search(password)
if m:
print ("Password cannot contain 3 or more of the same characters in a row\n")
sys.exit(0)
You need to prefix your regex with the letter 'r', like so:
s = re.compile(r'((\w)\2{2,})')
If you don't do that, then you'll have to double up on all your backslashes since Python normally treats backlashes like an escape character in its normal strings. Since that makes regexes even harder to read then they normally are, most regexes in Python include that prefix.
Also, in your included code your password isn't in quotes, but I'm assuming it has quotes in your code.
Can't you simply go through the whole string and everytime you found a character equal to the previous, you incremented a counter, till it reached the value of 3? If the character was different from the previous, it would only be a matter of setting the counter back to 0.
EDIT:
Or, you can use:
s = 'aaabbb'
re.findall(r'((\w)\2{2,})', s)
And check if the list returned by the second line has any elements.