【问题标题】:How to let Python recognize both lower and uppercase input?如何让 Python 识别小写和大写输入?
【发布时间】:2012-09-25 00:51:36
【问题描述】:

我是 Python 新手。我正在编写一个程序来区分单词是否以元音开头。问题是,该程序只能正确处理大写字母作为输入。例如,如果我提供单词“Apple”作为输入,则结果为True;但是,如果输入单词“apple”,则结果为False。我如何解决它?

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")

【问题讨论】:

    标签: python uppercase lowercase


    【解决方案1】:

    我写了一个简单的游戏运行代码希望你能理解

    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!")
    

    如果代码中有一些缩进错误,请使用 tab 并重新格式化代码行 我不知道如何在 StackOverflow 上准确编写代码,因为我是新来的

    【讨论】:

      【解决方案2】:
      swim_wait = input("Alright you reach the river.Do you want to 
      Swim or Wait\n")
      swim_wa = swim_wait.lower()
      

      我在程序中使用.lower() 将用户输入转换为较低的,这在处理输入时很方便。您也可以使用.upper() 但我认为你不能同时使用两者。

      【讨论】:

        【解决方案3】:
        1. 您只检查代码中的大写字母。一种方法是将用户的任何输入转换为大写字母,您当前的代码将起作用。这可以通过像这样更改您的代码来轻松完成...

          word = input("Please Enter a word: ").upper()
          
        2. 您还需要使用 word[0] 而不是 word[1] 来获取第一个字母。其余的代码可能是这样的:

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

        这将使第一个字母大写,其余字母保持原样。

        【讨论】:

          【解决方案4】:

          你应该使用:

          word[i] in 'AEIOUaeiou'
          

          【讨论】:

            【解决方案5】:

            通常你会在输入上使用str.lower()(或str.upper())来对其进行规范化。

            Python3.3 有一个名为str.casefold() 的新方法,它适用于 unicode

            【讨论】:

            • 哇,我不知道.casefold()。看起来很有用!
            【解决方案6】:

            元音检查是使用str.startswith 完成的,它可以接受多个值的元组。 PEP 8 Style Guide for Python Code 建议使用带有过字符串切片的startswith,以提高代码的可读性:

            使用 ''.startswith() 和 ''.endswith() 代替字符串切片 检查前缀或后缀。

            Conditional Expressions 用于设置单词是否以元音开头的消息。然后我使用String Formatting 方法准备消息。另外,作为英语语法校正的事情,我将句子“单词不以元音开头”替换为“单词不以元音开头”。

            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))
            

            【讨论】:

              【解决方案7】:

              首先将单词完全转换为小写(或大写):

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

              另外,要获取单词的第一个字母,请使用 word[0],而不是 word[1]。在 Python 和几乎所有编程语言中,列表都是零索引的。

              您还可以将代码压缩很多:

              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")
              

              【讨论】:

              • word[0].lower() 可能效率更高
              【解决方案8】:

              您可以在比较之前将输入转换为大写。

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 2020-03-30
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2017-01-02
                • 1970-01-01
                相关资源
                最近更新 更多