Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
if else statements
#9
If you want to compare a user-input of equality, but case-insensitive, then lower the user-input and compare it only with the lower variant of the string.

...
user_input = input("Do your choice: ")

if user_input == "b" or user_input == "B":
    print("User-Input was:", user_input)
else:
    print("Got a different input")
Same logic, but shorter:

...
user_input = input("Do your choice: ")

# first user_input.lower() is evaluated, then this is compared to "b"
# the user_input itself is imutable, so user_input.lower() creates a new string with only
# lower case characters
if user_input.lower() == "b":
    # here the original user_input is used, which is not lowered
    print("User-Input was:", user_input)
else:
    print("Got a different input")
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Messages In This Thread
if else statements - by remy - Sep-14-2018, 10:05 AM
RE: if else statements - by Mekire - Sep-14-2018, 10:12 AM
RE: if else statements - by remy - Sep-15-2018, 03:48 AM
RE: if else statements - by volcano63 - Sep-15-2018, 02:51 PM
RE: if else statements - by ichabod801 - Sep-15-2018, 08:09 PM
RE: if else statements - by volcano63 - Sep-15-2018, 08:42 PM
RE: if else statements - by gruntfutuk - Sep-16-2018, 01:17 PM
RE: if else statements - by LinkAiris - Apr-24-2024, 10:22 PM
RE: if else statements - by DeaD_EyE - Apr-25-2024, 07:27 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020