This question already has answers here:
Case insensitive 'in'
(11 answers)
Closed 6 years ago.
I'm a little confused regarding a Python exercise that requires a case insensitive input.
This part of the excercise that confuses me:
Make sure your comparison is case insensitive. If 'John' has been used, 'JOHN' should not be accepted.
Here is my code:
current_users = ['username_1', 'username_2', 'username_3', 'username_4',
'username_5']
new_user = ['new_user_1', 'new_user_2', 'new_user_3', 'username_1', 'Username_2']
for username in new_user:
if username in current_users:
print("Please enter a new username.")
elif username not in current_users:
print("This username is available.")
My problem is that I'm trying to get my code to reject "Username_2" because of the capital "U" but I have no idea how to do this. I'm reading through Python Crash Course by Eric Matthes and am currently on chapter 5 but I don't recall being taught how to reject case insensitive inputs yet.
I know of the upper(), lower(), and title() string methods and I tried using:
username.lower() == username.upper()
new_user.lower() == new_user.upper()
before my for loop, but this just results in a syntax error.
You can convert each new username to lower case and compare it to a lower case version of the user list (created using a list comprehension).
lower_case_users = [user.lower() for user in current_users]
if new_user.lower() in lower_case_users:
# Name clash
You may want to store your usernames in lowercase when they are first created to avoid creating a lower case version from the user list.
Related
This question already has answers here:
String contains any character in group?
(5 answers)
Closed last year.
I want to check if username has any Character from blockCharacter but i could'nt do it and this is my code
user = input("Whats your name\n").lower()
user = (user.title())
BlockChar = ["+","none","-"]
if user == BlockChar:
print("That does'nt not feels right. Try Again")
breakpoint
print ("Welcome " + user)
I am new to codeing and stack overflow so i need some help with it
The any function and a generator expression will work quite nicely to determine if any of the strings in BlockChar are in your user string.
if any(ch in user for ch in BlockChar):
...
This question already has answers here:
How do I do a case-insensitive string comparison?
(15 answers)
Closed 2 years ago.
I want to make my user's input (in Python) case insensitive. Like if user type admin, Admin ADMIN or even he or she types like aDMIN. This should be insensitive so i can get exact input
Use lower() : s = input().lower()
Convert your user's input to lower case. Then whatever you compare it to can be lower case.
string = input("Enter something: ")
string = string.lower()
Just use the lower() function, it converts a string to lower case.
myStr = JAMES
myStr.lower() > james
You can just compare it to lower case admin every time.
input_str = input("Enter role: ")
if input_str.lower() == "admin":
pass
This question already has answers here:
Asking the user for input until they give a valid response
(22 answers)
Closed 3 years ago.
I'm very new to python and code in general and I'm wondering how i can do something like this:
User enters string, computer checks if string is longer then 10 characters and if it is it asks for the user to enter a new string. This is what i have right now.
usernamelength = (len(username))
if usernamelength > 10:
return
else:
print("Hello, %s. Placeholder text." %username)
Sorry if i'm missing out something obvious, as i said earlier I'm new to this. Any help appreciated :)
This is a good place for a while loop instead of that if statement:
while usernamelength > 10:
# ask for the username again
# usernamelength = len(newUsername)
That way you'll just continue to prompt for the username until you're given something over 10 chars.
This question already has answers here:
Comparing True False confusion
(3 answers)
Closed 5 years ago.
As part of an assignment we've been asked to create a very basic/elementary program that asks for user input (whether they desire coffee or tea, the size, and whether they desire any flavourings), and then outputs the cost of the particular beverage, including their name and what they ordered, in addition to the cost. The code I wrote works perfectly; however, the only question I have is more for my own understanding. Our instructions for the customer's name were as follows: "The customer’s name – A string consisting of only upper and lower case letters; no
spaces (you may assume that only contains letters of the alphabet)."
Thus my code was as follows:
customerName = str(input('Please enter your name: '))
if customerName.isalpha() == False:
print('%s is an invalid name, please try again!' % customerName)
else:
And then I just continue from there - however, PyCharm is telling me "expression can be simplified - this inspection detects equality comparison with a boolean literal" regarding the
if customerName.isalpha() == False:
statement. What would be the best way to simplify this?
You can use the result of str.isalpha directly; it's a boolean!:
if not customerName.isalpha():
print('%s is an invalid name, please try again!' % customerName)
This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
Multiple if statements under one code, with multiple conditons [duplicate]
(3 answers)
Closed 8 years ago.
It is pretty self-explanitory from the code but I want to check if the input is not equal to these values than ask again. I thought this would work but it doesn't and its glitchy, what is a better way to do this?
type=input("Please choose an option: ")
while type.isalpha() == False:
type=input("Please choose an option: ")
while type != ("a" and "A" and "b" and "B" and "c" or "C"):
type=input("Please choose an option: ")
Simply do while not type in ("a","A","b","B" ...) to check whether type is one of the listed elements.
The code above is, as mentioned in comments, equivalent to while type != someListElement because the and and or are evaluated first.
You would need to write:
while (type != "a" and type !="A" and type !="b" and type !="B" and type !="c" or type !="C"):
I think the simplest solution would be to use
type = raw_input("Please choose an option: ")
while type not in list('AaBbCc'):
type = raw_input("Please choose an option: ")
list will convert from a string to a list of single-character strings, which you can then test for inclusion using in. I don't think you need the test for isalpha, because everything you're checking for is already a letter.
Also, you should always use raw_input rather than input to get user input, because raw_input always returns a string, while input tries to eval what the user enters, which is not what you want.
(This is assuming you're using Python 2. If you're using Python 3, input is what raw_input was before, and raw_input no longer exists.)