Symbol-only string detection - python

I'm fairly new to Python (and programming in general), so I often end up facing really silly issues, such as the one below.
What I want is to repeatedly check if all the characters in a user input are symbols. When such an input is entered, I want to print that string.
Since there doesn't seem to be a way to specifically test for symbols, I decided to test for letters first, and then for numbers, and if both of them come up as negative, then it should print the text.
Here is my code:
while True:
symbols = input("Enter symbol string:")
if symbols == symbols.isalpha():
print (symbols.isalpha())
elif not a == a.isalpha():
print (a.isalpha())
break

Symbols is a little vague but here is a strategy:
symbols = input("Enter symbol string:")
valid_symbols = "!##$%^&*()_-+={}[]"
has_only_symbols = True
for ch in symbols:
if ch not in symbols:
has_only_symbols = False
break
if has_only_symbols:
print('Input only had symbols')
else:
print('Input had something other than just symbols')
The above code first creates a list of symbols which you want to ensure the string is created out of called valid_symbols. Next it creates a variable called has_only_symbols that holds a value of True to begin with. Next it begins to check that each character in the input string exists in valid_symbols. If it hits a character which is invalid then it changes the value of has_only_symbols to False and breaks out of the for loop (no need to check the rest of the string). After the loop is done it checks whether has_only_symbols is True or False and does something accordingly.
Also as a side not, some of your code is not doing what you think it is:
if symbols == symbols.isalpha():
...
will test if symbols, your input string, is equal to the result of symbols.isalpha() which returns a boolean True or False. What you probably meant is just:
if symbols.isalpha():
...
The elif statement is strange as well. You have begun referencing some variable called a but you do not have it defined anywhere in the code you posted. From your description and code it seems you meant to have this elif statement also reference symbols and call the isdigit method:
if symbols.isalpha():
...
elif symbols.isdigit():
...
else:
...
However this is not logically complete as a string with mixed letter, digit, and symbol will slip through. For example abc123!## will fail both the tests and get printed. You want something more exclusive like the above code I have written.

This is how i solved it..
import re
def start():
global count
for letter in SYMBOLS:
if re.search(reg,SYMBOLS):
count=count+1#just to count how many exist
global S#if you want to store the result
S=letter
else:
print(letter,': is not a symbol')
count = 0
SYMBOLS= input('Enter text\n')#INPUT
reg =('[#_!#$£%^&*()<>?/\|}{~:]')#SYMBOL SEARCH
start()

Slightly updated version of Farmer Joe's code. Better processing of input included.
symbols = input("Enter any characters:")
valid_symbols = "!##$%^&*()_-+={}[]"
if symbols == '':
print("None of characters have been entered. Please try again")
else:
for ch in symbols:
if symbols.isalnum() is True:
print('No symbols have been detected in the input')
break
else:
if ch not in valid_symbols:
print('There are symbols mixed with other characters')
break
else:
print('Your input contains symbols only')

Related

Username and Password validation with user input

Im very new to python and I am creating a user login system, I am currently on a bit of creating a username and password with user input that must meet some conditions e.g
username:
Cannot contain any spaces
Must be at least 5 characters
Cannot include special characters
Your system must display a message to the user telling them what they did wrong if they did not meet
one or more of these criteria (so you will need at least 4 error messages).
My code is as below, but surely theress a better way to do this?
while True:
sNewUser1 = input("""Please enter a new username.
The username must NOT contain any spaces, it must have at least 5 characters and
it cannot include any special characters: \n\n""")
if len(sNewUser1) < 5:
print("Your username is too short, please enter 5 or more characters! Please try again!\n")
elif sNewUser1.count(" ") > 0:
print("Your username contains one or more spaces, this is not allowed! Please try again! \n")
elif sNewUser1.isalnum() == False:
print("Your username contains a special character please try again! \n")
else:
greetuser()
break
while True:
sNewPass1 = input("""\n\nPlease enter a new password.
It must contain:
At least one Capital letter
At least one lower case letter
At least one special character
It has to be at least 6 characters in length:\n\n""")
if len(sNewPass1) < 6:
print("Your username is too short, please enter 5 or more characters! Please try again!\n")
Input prompts can be shortened and more direct.
The whole code should be wrapped in a main() function which is called at the end using the if __name__ == "__main__" condition. This is a convention.
Use is with boolean values, in those conditional statements.
Use the snake_case naming style.
The long strings can be broken into multiple smaller ones.
By now, linters and code-formatters (even Pylint) will not complain about anything.
Your complete code may look something like this, although there is still room for improvement:
"""This is a program for validating usernames and passwords"""
def main():
"""This is the main function"""
while True:
new_user_1 = input("\nPlease enter a new username. "
"It should be at least 5 characters long "
"and not contain spaces or special characters: ")
if len(new_user_1) < 5:
print("Your username is too short. Please try again: ")
elif new_user_1.count(" ") > 0:
print("Your username contains spaces. Please try again: ")
elif new_user_1.isalnum() is False:
print("Your username contains a special character. "
"Please try again: ")
else:
# call another function
break
while True:
new_pass_1 = input("\nPlease enter a new password. "
"It should be 6 characters long "
"with atleast one uppercase letter, "
"one lowercase letter, and one special character: ")
if len(new_pass_1) < 6:
print("\nYour password is too short. Please try again: ")
elif any(lower.islower() for lower in new_pass_1) is False:
print("\nYour password does not contain lowercase letters. "
"Please try again: ")
elif any(upper.isupper() for upper in new_pass_1) is False:
print("\nYour password does not contain uppercase letters. "
"Please try again: ")
elif any(digit.isdigit() for digit in new_pass_1) is False:
print("\nYour password does not contain digits. "
"Please try again: ")
elif any(not char.isalnum() for char in new_pass_1) is False:
print("\nYour password does not contain special characters. "
"Please try again: ")
elif new_pass_1.replace(" ", "") != new_pass_1:
print("\nYour password contains whitespaces. "
"Please try again: ")
else:
# call another function
break
if __name__ == "__main__":
main()
Edit: The previous answer had some bugs. The new one works as intended:
Strings were broken down into smaller ones, but spaces in the end were ommitted.
Simple for loops were used, which checked whether the whole string was uppercase, lowercase, digits, special characters or not, instead of checking each character in the string. The new code fixes this with the use of a built-in function called any. From Python's official documentation:
any(iterable):
Return True if any element of the iterable is true. If the iterable is empty, return False. Equivalent to:
def any(iterable):
for element in iterable:
if element:
return True
return False
Here is an explanation of why/how this works:
elif any(lower.islower() for lower in new_pass_1) means: for any lower in new_pass_1, if lower.islower() returns True, then: code goes here. Therefore, it returns True if any character in the string is lowercase. So if it returns False, it would mean that it did not find any lowercase character in the whole string. The same applies to the checks for uppercase letters and digits.
elif any(not char.isalnum() for char in new_pass_1) means: for any char in new_pass_1, if char.isalnum() does not return True (i.e. the char is a special character), then: code goes here. Therefore, it returns True if any character in the string is a special character (i.e not an uppercase letter, lowercase letter or digit). So if it returns False, it would mean that it did not find any special character in the whole string.
If you're confused about not char.isalnum(): any_string.isalnum() checks whether any_string is alphanumeric or not, i.e whether it is made up of alphabets and numbers only. So inverting it with not char.isalnum() would now check whether any_string is not alphanumeric. And we know that if something is not alphanumeric, it is a special character. Note that this will consider whitespaces to be special characters as well, so I've added a final if statement to check for whitespaces.

Character checking function not functioning

I'm working on learning by making a hangman game and I'm trying to validate user input to require a user enter a five character word, with no spaces, special characters or numbers.
Everything worked until I defined the containsspec() function and tried to implement it.
Here's the main game file, and then the file it's calling which contains the functions.
#text-based hangman project
#import defined functions from fun.py
from fun import *
#get player1 input and validate that it's at least five characters long, with no numbers or spaces
print("Please select any word with five characters or more:")
c = 0
while c == 0:
w = getpass.getpass(prompt = "")
w = list(w)
if len(w) < 5 or containsnumber(w) or containsspace(w) or containsspec(w):
print("Your input is invalid. Please try again.")
else:
print("Got it - thank you!")
c += 1
File that contains the functions:
#contains functions for 1.py
import getpass
def containsnumber(value):
for character in value:
if character.isdigit():
return True
return False
def containsspace(value):
for character in value:
if character == " ":
return True
return False
def containsspec(value):
for character in value:
if character.isalnum():
return False
return True
So if w contains a character that isn't alphanumeric, it should return True, and the main game should print "Your input is invalid. Please try again. Right?
I'm just super confused about why this isn't working. I'm learning, so I'm not interested in hearing about how I could change the whole code. I'm really interested in just why containsspec() isn't working.
Try this:
def containsspec(value):
return not all(character.isalnum() for character in value)
I believe your issue is that you are immediately returning false when you detect an alphanumeric character, which isn't what you want because you need to make sure every letter is alphanumeric.
The code above applies a boolean .isalnum() to each character in value, and if every character is alphanumeric, it will return False (indicating that it does not contain special characters), else it will return True. Notice the not! That is important!
You could also make it say any(not character.isalnum() for character in value) -- the two are equivalent.

Isspace function in python

I'm having trouble trying to execute this code, I want the user to input a value, the program checks if that value is a string then it returns the length.
If the value contains whitespaces the programs remove the whitespace and print the length.
But if it contains any integer values the program returns "No Integers is Allowed"
This is the code:
def length(Name):
long = len(Name)
return long
new_length = input("Please Enter Your name You can use Spaces: ")
value1 = new_length
if value1.isspace() == True:
print("This is Before Removing Spaces: " + value1)
value2 = value1.replace(" ", "")
print("This is After Removing Spaces: " + value2)
elif value1.isalpha() == True:
print("Here is The Length: ", length(value1))
elif value1.isdigit() == True:
print("Integers are not allowed! ")
else:
print("There's someting wrong with "+ value1)
So if you can help me with that I appreciate it.
Thanks
I don't think the str.isspace, str.isalpha and str.isdigit methods do what you expect them to do. To start with, they all test if all the characters in the string you enter are of the type that is described in their name. Your code seems to be expecting them to be return True if any of the characters match. That is, if there are any spaces, you want to remove them and show the two lengths, before and after.
There's no single string method that will do that test for you in Python. You could use regular expressions (which are more powerful, but much more complicated), or you could write some slightly more elaborate code to do the test. I'd suggest using the any function, and passing it a generator expression that calls the method you want on each character in the string.
if any(c.isspace() for c in user_str):
...
This may not be exactly what you want for all of your tests. The desired logic of your code is not entirely obvious, as there are a number of corner cases that your output doesn't specifically address. Is a string that contains both letters and numbers valid? How about one that has spaces in between numbers, but no letters at all? You may need to reorder the conditions of your if/elif/else statements so that they match what you intend.
I'd also note that the variable name you used for user input, new_length, is very misleading. It's not a length, its the string you want to measure the length of! It's a lot easier to make logic errors about variables that have misleading or unclear variable names, so taking time to go back and reconsider names you chose earlier is sometimes a good idea, as it can improve the clarity of your code a lot! Descriptive variable names are good, but it's a tradeoff between clarity and brevity, as long names are tedious to type (and prone to typos). They also can lead to line length issues, which can make it less convenient to see all your code on your editor screen at once.
You can use this function to check if the input string contains a number:
def hasNumbers(inputString):
return any(char.isdigit() for char in inputString)
It returns true if there is a number and false if there is not.
As for the whitespaces you can ommit isspace(). Using replace() alone will do the job, even if there are no whitespaces.
stri='jshsb sjhsvs jwjjs'
stri=stri.replace(' ','')
I suggest reading the documentation in these cases. For isspace, note here that
Return True if there are only whitespace characters in the string and there is at least one character, False otherwise.
That is, if there's anything that's not a space there, it will be False. Which is annoying, but why check in the first place? Just do the replacement! If there's no whitespace, it won't do nothing. If you need to print those statements, you can do
if ' ' in value1:
...
(of course, this doesn't consider all the possible kinds of whitespaces, check the other answers for doing the for loop if you need that)
Next, I believe you need to remove the elifs and just use if statements, since note that if you input a name with a space, it will print the name with the spaces removed... and nothing after that. Not even if it has integers in it. This is because elif statements don't execute once another above them did.
There are many other things you need to consider, but these two I think you should consider first. Hope it's useful!
You can use the re module in Python to check for white spaces in your string. It returns True if there are white spaces and False otherwise.
import re
def length(Name):
long = len(Name)
return long
new_length = input("Please Enter Your name You can use Spaces: ")
value1 = new_length
if re.search('\s', value1):
print("This is Before Removing Spaces: " + value1)
value2 = value1.replace(" ", "")
print("This is After Removing Spaces: " + value2)
print("Here is The Length: ", length(value2))
elif value1.isalpha() == True:
print("Here is The Length: ", length(value1))
elif value1.isdigit() == True:
print("Integers are not allowed! ")
else:
print("There's someting wrong with "+ value1)

I am trying to write the python code that can check repeat word in sentance

for this code i try to find if the word is repeat it will say word is "not unique" and if word does not repeat it will say word is "unique". I run the program but it showing error after i enter the sentence.
def isUnique():
words = sentence.split()
unique = {}
for i in words:
if word not in unique:
count[i] = true
else:
unique[i] = false
def main():
user_input = input("Enter word sperated by space: ")
uniques = isUnique(user_input)
print(uniques)
main()
There are loads of things causing errors in your code. I suggest at least learning the basics of how to define and call a function before trying to create one. Also you need to learn about variables and how to correctly reference them, and how to define boolean values.
The issues here are things you should have read about before writing the code.
Issue 1: Indentation
You haven't indented your code properly. You need to indent after you start a for loop (and everything within the for loop must remain indented. You need to indent after every if/elif/else statement, and everything pertinent to the statement must remain indented. Proper indentation of your code looks like this:
def isUnique():
words = sentence.split()
unique = {}
for i in words:
if word not in unique:
count[i] = true
else:
unique[i] = false
def main():
user_input = input("Enter word sperated by space: ")
uniques = isUnique(user_input)
print(uniques)
main()
Issue 2: No argument in isUnique()
When you define a function, if it requires an argument, then you need to specify the argument when you create the function. Like this:
def isUnique(sentence):
words = sentence.split()
unique = {}
for i in words:
if word not in unique:
count[i] = true
else:
unique[i] = false
Here, sentence will be the same as user_input when you call the function. This way, when the computer sees sentence inside the function, it knows that sentence is the argument in the function (i.e. the user input). The way you've written it, how should the computer know what sentence is if it's not in the function argument? Answer: it doesn't.
Issue 3: The loop iterator
With the for loop, you call each object you're iterating over i. But then inside the for loop, you refer to this object as word. Again, how can the computer know what word is if you haven't defined it. This is the correct way:
for word in words:
if word not in unique:
count[i] = true
else:
unique[i] = false
Issue 4: What is count?
Again, inside the for loop you reference a dictionary called count, but you haven't defined anything called count. Maybe you mean:
for word in words:
if word not in unique:
unique[i] = true
else:
unique[i] = false
Issue 5: Booleans start with a capital
Python's booleans should be True and False, not true and false.
Now the errors should be gone, but I'll leave the rest up to you. I'm not sure that the code will solve your problem correctly as it is.
But you need to learn the basics. Before you start learning about data structures like dictionaries, you should be very adept with the use of variables, for loops and if statements, which this question demonstrates that you are not. Please learn these things.

Changing inputs into titles, issue with handling raw spaces

so I'm quite new to programming and I'm trying to learn python as a starter.
I'm trying to make a function that does multiple things (I'm going to use it for limiting inputs on names).
Rejects purely numerical inputs
Rejects inputs made purely of spaces
Rejects null inputs
Changes the input into a title
def debugstr(inputa):
inputa = inputa.replace(" ", "")
try:
int(inputa)
inputb = debugstr(input("Invalid input, please enter Alphabetic strings only: "))
except:
if inputa == "":
debugstr(input("Invalid input, please enter Alphabetic strings only: "))
else:
return inputa.title()
The issue that I have is that the code will only reject blank inputs on the first try when running the function, if something is rejected once and the user inputs a series of spaces again, then it will just accept it as an input.
Thanks for your time in advance! It's very appreciated :D
A more natural way of handling this (without calling the same function from within itself) is:
def make_title():
def get_user_input():
return input('Enter an alphabetic string: ')
while True:
s = get_user_input()
s = s.strip()
if not s:
print('blank input!')
continue
if s.isdigit():
print('contains only digits!')
continue
return s.title()
print(make_title())
Some notes:
Try not to repeat yourself (e.g. the duplicated error message in your code)
Python contains many useful string methods and s.isdigit() returns True if s contains only numbers
You can strip the whitespace from your input with s.strip() and if you're left with the empty string, '', if not s will be True (the empty string is equivalent to False.
In python 3, you can use isinstance to check if an object is a string.
word = input("Enter string: ")
def checkString(s):
if isinstance(s, str):
print('is a string')
elif not s:
print('empty')
else:
print('not a string')

Categories