区分大小写字母的程序

问题描述 投票:0回答:1

问题是—— 请编写一个程序来询问用户正在使用哪个编辑器。程序应继续询问,直到用户在 Visual Studio Code 中键入。如果用户在 Word 或记事本中键入,则程序会出现 bad 计数器。其他不可接受的编辑选择收到的答复不好。

程序的反应应不区分大小写。也就是说,相同的用户输入小写、大写或混合大小写应该触发相同的反应

预期结果-

Editor: Atom
not good
Editor: Visual Studio Code
an excellent choice!
Editor: NOTEPAD
awful
Editor: viSUal STudiO cODe
an excellent choice!

我的代码-

mystring = 'Visual Studio Code'
mystr2 = 'Notepad'
mystr3 = 'word'
while True:
    usr_input = input('Editor:')
    if mystring in usr_input or mystring.upper() in usr_input or mystring.lower() in usr_input:
        print('an excellent choice!')
        break
    elif mystr2 in usr_input or mystr2.upper() in usr_input or mystr2.lower() in usr_input:
        print('awful')
    elif mystr3 in usr_input or mystr3.upper() in usr_input or mystr3.lower() in usr_input:
        print('awful')
    else:
        print('not good')

当我输入“visual STudiO code”或“nOtEpAd”时,程序打印出“不好”而不是“一个绝佳的选择!”或“可怕”

python case-sensitive
1个回答
0
投票

您需要使用

lower()
将用户输入和其他变量转换为小写,然后进行比较。这个效果很好:

mystring = 'Visual Studio Code'
mystr2 = 'Notepad'
mystr3 = 'word'
while True:
    usr_input = input('Editor: ')
    if usr_input.lower() == mystring.lower():
        print('an excellent choice!')
        break
    elif usr_input.lower() == mystr2.lower() or usr_input.lower() == mystr3.lower():
        print('awful')
    else:
        print('not good')
© www.soinside.com 2019 - 2024. All rights reserved.