List in python, heeeeelp meeeee sensie | SoloLearn: Learn to code for FREE!

+2

List in python, heeeeelp meeeee sensie

myList = ["Nani", "onichan", "kodasai"] How do i get rid of these errors? What i am going to do here is to find the exact index number of my list even i put lower cases string. Example, Sample = myList.index("nani" ) This turns into error result since nani is not in the list unless, the "n" will be capitalized. I already use .lower() function while converting the list of myList into lower cases but i put it in another variable and it works, but is there another way to make this simple and not complicated?

2/28/2021 9:53:21 AM

Kakai

15 Answers

New Answer

+2

You should lowercase the way i updated in the code. mylist[i]=mylist[i].lower() Explanation: Because only this will lowercase the character and will updated the value lowercased in the list concurrently

+11

mylist=["Nani","onichan","kodasai"] mylist=list(map(str.lower,mylist)) x=mylist.index("nani") print(x)

+7

You getting check name as input if you want your code to work all situation regardless of user input converts checkname to lower mylist=["Nani","onichan","kodasai"] mylist=list(map(str.lower,mylist)) check=input() x=mylist.index(check.lower()) print(x) if you want to avoid error when name not found use find method mylist=["Nani","onichan","kodasai"] mylist=list(map(str.lower,mylist)) check=input() x=mylist.find(check.lower()) print(x)

+2

May be....it should work. Sample = myList.index("nani".lower())

+2

Abhay here *input a teammate names "but capitalized" *input checker in "lower cases" Sample: Name: KENT, BEN, YEN checker: kent Output// nope list = [] print("Enter teammates") for i in range(3): list += [input("Enter name: ")] while True: check = input("\nCheck name: ") if check in list: print("Present") else: print("Nope")

+2

Nihar Buliya yup, its mine.

+2

Ok if that's the exact code. It is showing error if i am checking for "nani" in the list. The error message is "nani is not in the list" so the question here is what are you using to lowercase the list elements?

+2

Nihar Buliya so, how do i get rid of that thing?

+2

Also you can try use a method to change the string itself my_list = ['Nani', 'Onichan', 'Kodasai'] Sample = my_list.index('nani'.capitalize()) capitalize () it's the method to make only the first letter to Capital Capital letter

+1

Naveen Rathore nope, u r just trying to lower the cases of your sample variable. also in that case, your sample variable is nothing to do with the list. U r doing this >> ...("NANI".lower()) >> output: nani >> sample.

+1

I didn't understand what exactly you are looking for but code by Naveen Rathore works fine

+1

Above is another example, it always turns to nope if i put lower cases in checker name

+1

Kakai so can't you make names lowercase when user adds an input , like list += [input("Enter name: ").lower()] Or list should only have the same case of letters as input ?

+1

It is showing the same error for me even if i am putting the mylisy.index() into variable..Can you check if that was your code? https://code.sololearn.com/c7A083A6A9a1/?ref=app

0

Run a loop for list and convert all characters to lowercase and then you can get the index .