Jump to content

How to generalize upper and lower case input in Python

Go to solution Solved by HunterAP,

Like Nocte said, you can use the upper() and lower() functions.

Basically you take the user input, make it lowercase using the lower() function, then compare it to your options in lowercase, like so:
 

# Creating decision maker
import random
a = input("Magic 8 Ball, Do you want any advice?\n")
if a.lower() == "yes":
	b.lower() = input("Type 'shake' to activate Magic 8 Ball\n")
	if b == "shake":
		c = input("Ask me anything and you shall get the answer, press enter after you have it in your mind\n")
		print(random.choice(["Surely", "Most Probably", "Nop", "Louder man", "You might wanna think about that", "Maybe", "Probably", "Maybe not"]))
	else:
		exit()
elif a == "no":
	print("Okay, goodbye; See ya Later")
elif a == "":
	print("Type something man")

I also added proper indentation. You also don't need the 

if c == "":

statement because the code is supposed to give a response regardless of the question.

Lastly, you didn't need the 

elif b == "":

code snippet and instead can make it into a simple "else" statement.

# Creating decision maker
import random
a = input("Magic 8 Ball, Do you want any advice?\n")
if a == "yes":
b = input("Type 'shake' to activate Magic 8 Ball\n")
if b == "shake":
c = input("Ask me anything and you shall get the answer, press enter after you have it in your mind\n")
if c == "":
print(random.choice(["Surely", "Most Probably", "Nop", "Louder man", "You might wanna think about that",
"Maybe", "Probably", "Maybe not"]))
elif b == "":
exit()
elif a == "no":
print("Okay, goodbye; See ya Later")
elif a == "":
print("Type something man")
	

 

I want to generalize the casing of the user input, whether they type in upper or lower case..

 

 

Keeping the article simple, thanks in advance.. :)

PC

 

CPU : Ryzen 3 2200G

RAM: G SKILL Ripjaws 8GB 2400Mhz

MoBo: GigaByte A320M - HD2

Storage: WD Green SSD

Case: Cooler Master 

PSU: Cooler Master Elite 400W

Link to post
Share on other sites

You can use the upper() and lower() methods:

 


a = "HeLLo"

print(a.lower())

>>> hello

CPU: Ryzen 7 9800X3D Cooler: Noctua NH-D15 Black Mobo: Gigabyte X870E Aorus Pro GPU: Sapphire Nitro+ 9070 XT RAM: G.Skill 2x16GB @ 6400 MHz SSD: PNY XLR8 2TB PSU: Corsair RM1000x Case: Fractal Design North Monitor 1: Asus XG27AQWMG(280Hz) Monitor 2: Asus VG259QM (240Hz)

I usually edit my posts immediately after posting them, as I don't check for typos before pressing the shiny SUBMIT button.

Unraid Server

CPU: Ryzen 5 7600 Cooler: Noctua NH-U12S Mobo: Asus B650E-i RAM: Kingston Server Premier ECC 2x32GB (DDR5) SSD: Samsung 980 2x1TB HDD: Toshiba MG09 1x18TB; Toshiba MG08 2x16TB HDD Controller: LSI 9207-8i PSUCorsair SF750 Case: Node 304

Link to post
Share on other sites

Like Nocte said, you can use the upper() and lower() functions.

Basically you take the user input, make it lowercase using the lower() function, then compare it to your options in lowercase, like so:
 

# Creating decision maker
import random
a = input("Magic 8 Ball, Do you want any advice?\n")
if a.lower() == "yes":
	b.lower() = input("Type 'shake' to activate Magic 8 Ball\n")
	if b == "shake":
		c = input("Ask me anything and you shall get the answer, press enter after you have it in your mind\n")
		print(random.choice(["Surely", "Most Probably", "Nop", "Louder man", "You might wanna think about that", "Maybe", "Probably", "Maybe not"]))
	else:
		exit()
elif a == "no":
	print("Okay, goodbye; See ya Later")
elif a == "":
	print("Type something man")

I also added proper indentation. You also don't need the 

if c == "":

statement because the code is supposed to give a response regardless of the question.

Lastly, you didn't need the 

elif b == "":

code snippet and instead can make it into a simple "else" statement.

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×