user1718826
user1718826

Reputation: 347

How to let Python recognize both lower and uppercase input?

I am new to Python. I am writing a program that distinguishes whether or not a word starts with a vowel. The problem is, that the program is only able to correctly handle uppercase letters as input. For example, if I provide the word "Apple" as input, the result is True; however, if the word "apple" is provided as input, the result is False. How do I fix it?

word = input ("Please Enter a word:")
if (word [1] =="A") :
    print("The word begins with a vowel")
elif (word [1] == "E") :
    print("The word begins with a vowel")
elif (word [1] == "I") :
    print("The word begins with a vowel")
elif (word [1] == "O") :
    print("The word begins with a vowel")
elif (word [1] == "U") :
    print("The word begins with a vowel")
else:
    print ("The word do not begin with a vowel")

Upvotes: 7

Views: 99643

Answers (8)

Ahmad Hassan Khan
Ahmad Hassan Khan

Reputation: 1

I have written a code for a simple game run it I hope you will understand

print("Welcome to Treasure Island\nYour Mission is to Find the Treasure\n")
choice1 = input('You are at the crossroad ,where do you want to go.Type "left" or "right".\n').lower()
if choice1 == "left":
    choice2 = input(
        'You\'ve come to a lake.There os a island in the middle of the lake.Type "wait"to wait for the boat.Type "swim" to sim across\n').lower()
    if choice2 == "wait":
        choice3 = input(
        "You arrive at the island unharmed. There is a house with 3 doors. One red, one yellow, and one blue. Which color do you choose?\n").lower()
       if choice3 == "red":
            print("Its room full of fire. Game Over!")
        elif choice3 == "yellow":
            print("You found the treasure you won!")
        elif choice3 == "blue":
            print("You enter a room of beasts.Game over!")
    else:
        print("You chosen the wrong door. Game over!")

If there is some indentation error in the code use tab and reformat the line of code I don't know how to exactly write code on StackOverflow as I am new here

Upvotes: -1

Ahmad Hassan Khan
Ahmad Hassan Khan

Reputation: 1

swim_wait = input("Alright you reach the river.Do you want to 
Swim or Wait\n")
swim_wa = swim_wait.lower()

I am using .lower() to convert user input to lower in program and this was convenient in dealing with input. You can also using .upper() but I think that you cannot use both at same time ok .

Upvotes: 0

Kapil Marwaha
Kapil Marwaha

Reputation: 1059

  1. You are checking only for the upper case in your code. One way would be to convert any input by the user to upper case and your current code will work. That can be easily be done by changing your code like so ...

    word = input("Please Enter a word: ").upper()
    
  2. You also need to use word[0], instead of word[1] to get the first letter out. The rest of the code could be like this:

    if word [0] in "AEIOU" :
        print("The word begins with a vowel")
    else:
        print ("The word does not begin with a vowel")
    

This will make the first letter in upper case and the rest will remain as is.

Upvotes: 0

Harrt
Harrt

Reputation: 21

You should use:

word[i] in 'AEIOUaeiou'

Upvotes: 2

John La Rooy
John La Rooy

Reputation: 304355

Usually you would use str.lower() (or str.upper()) on the input to normalise it.

Python3.3 has a new method called str.casefold() which works properly for unicode

Upvotes: 5

Marwan Alsabbagh
Marwan Alsabbagh

Reputation: 26828

The check for vowels is done using str.startswith which can accept a tuple of multiple values. PEP 8 Style Guide for Python Code recommends the use of startswith with over string slicing for better readability of code:

Use ''.startswith() and ''.endswith() instead of string slicing to check for prefixes or suffixes.

Conditional Expressions are used to set the message indicating whether the word starts with a vowel or not. Then I used the String Formatting method to prepare the message. Also just as a English grammar correction thing I replaced the sentence "The word do not begin with a vowel" with "The word does not begin with a vowel".

word = input("Please Enter a word:")
is_vowel = 'does' if word.lower().startswith(tuple('aeiou')) else 'does not'
print("The word {} begin with a vowel".format(is_vowel))

Upvotes: 0

Blender
Blender

Reputation: 298384

Convert the word entirely to lowercase (or uppercase) first:

word = input("Please Enter a word:").lower()  # Or `.upper()`

Also, to get the first letter of your word, use word[0], not word[1]. Lists are zero-indexed in Python and almost all programming languages.

You can also condense your code by quite a bit:

word = input("Please Enter a word:")

if word[0].lower() in 'aeiou':
    print("The word begins with a vowel")
else:
    print("The word do not begin with a vowel")

Upvotes: 5

Matt Ball
Matt Ball

Reputation: 359966

You could convert the input to upper case before comparing.

Upvotes: 1

Related Questions