如何让Python识别输入的大小写?

时间:2018-12-31 01:04:09

标签: python

我现在正在做作业,想让python识别输入的大小写。这是脚本:

print("Welcome to the student checker!")

student_lists={"Mary A","Tony Z","Sophie L","Zoe J","Joey M","Connie O","Olivia L","Mark Z","Donny M"}

while True:

    name=input("Please give me the name of a student (enter 'q' to quit):")

    if name in student_lists:
        print("Yes, that student is enrolled in the class!")

    else:
        print("No, that student is not in the class.")

    if name=="q":
        break

print("Goodbye!")

请让我知道如何解决它。非常感谢!

2 个答案:

答案 0 :(得分:0)

首先,将student_lists中的所有元素转换为小写字母:

student_lists = {name.lower() for name in student_lists}

然后代替

if name in student_lists:

使用

if name.lower() in student_lists:

在比较中将所有字母都小写。

答案 1 :(得分:0)

#Making a copy of the student_list list and parsing all names as lowercase.
student_list_lowercase = []
for item in student_list:
    student_list_lowercase.append(item.lower())

name=input("Please give me the name of a student (enter 'q' to quit):")

#Testing that the lowercase version of the student's name is present in the newly created list.
if name.lower() in student_lists_lowercase():
    print("Yes, that student is enrolled in the class!")

else:
    print("No, that student is not in the class.")

if name=="q":
    break