Reputation: 11
There is a piece of code that changes anything the user inputs to lower case, how would I implement this into my code instead of using ["a" or "A"]?
def displaymenu():
print("Weather station")
print("Please enter the option you would like")
optionchoice = input("Please enter A,B,C or D:")
if optionchoice in ["a" or "A"]:
print("The temperature will be displayed")
time.sleep(1)
optionA()
elif optionchoice in ["b" or "B"]:
print("The wind speed will be displayed")
time.sleep(1)
optionB()
elif optionchoice in ["c" or "C"]:
print("The day and time will be displayed")
time.sleep(1)
optionC()
elif optionchoice in ["d" or "D"]:
print("The Location will be displayed")
time.sleep(1)
optionD()
else:
print("Please type a valid input")
displaymenu()
Upvotes: 1
Views: 108
Reputation: 2612
Using str.lower() method, you could make changes to your code like so:
def displaymenu():
print("Weather station")
print("Please enter the option you would like")
optionchoice = input("Please enter A, B, C or D: ").lower() # Convert input to lowercase.
if optionchoice == 'a':
print("The temperature will be displayed")
time.sleep(1)
optionA()
elif optionchoice == 'b':
print("The wind speed will be displayed")
time.sleep(1)
optionB()
elif optionchoice == 'c':
print("The day and time will be displayed")
time.sleep(1)
optionC()
elif optionchoice == 'd':
print("The Location will be displayed")
time.sleep(1)
optionD()
else:
print("Please type a valid input")
displaymenu()
If for some reason you want to stick with your version, validate the input like so:
if optionchoice in ['a', 'A']:
Upvotes: 0
Reputation: 567
Actually Python has a built in method:
For example:
string = "ABCDabcd123"
lowercase = string.lower()
print(lowercase)
will give you abcdabcd123 !
Try it out and find more about it here: https://www.tutorialspoint.com/python/string_lower.htm
Upvotes: 0
Reputation: 1148
Try something like this:
optionchoice = input("Please enter A,B,C or D:").lower()
This way you are forcing the input to the lowercase version of whatever the user types.
Upvotes: 1