0

I am doing my homework now and want to make python recognize both lower and uppercase for the input. Here is the script:

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

Please let me know how to fix it. Much appreciated!!

Mad Physicist
  • 76,709
  • 19
  • 122
  • 186
Lyn S
  • 7
  • 3

2 Answers2

0

First, convert all elements in the student_lists set to lowercase letters:

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

and then instead of

if name in student_lists:

use

if name.lower() in student_lists:

to have all letters lowercase in the comparison.

MarianD
  • 9,720
  • 8
  • 27
  • 44
  • `lower()` is *almost* the right answer, but will not behave correctly with various internationalized strings. `str.casefold()` is what you probably should be using (see Veedrac's answer in the proposed duplicate). – Daniel Pryden Dec 31 '18 at 01:15
  • Thank you guys!! i made all the names lowercases in student_lists and used the lower(), which is working now!! – Lyn S Dec 31 '18 at 02:01
  • @LynS, the preferred form of "thank you" in StackOverflow is voting up the answer. – MarianD Jan 06 '19 at 02:02
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
Ciccarello
  • 38
  • 1
  • 6
  • `lower()` is *almost* the right answer, but will not behave correctly with various internationalized strings. `str.casefold()` is what you probably should be using (see Veedrac's answer in the proposed duplicate). – Daniel Pryden Dec 31 '18 at 01:17
  • Thanks! str.casefold() works as well! Perfect!! :) – Lyn S Dec 31 '18 at 02:04