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 AnswerYou 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
mylist=["Nani","onichan","kodasai"] mylist=list(map(str.lower,mylist)) x=mylist.index("nani") print(x)
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)
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")
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?
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
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.
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 ?
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
Run a loop for list and convert all characters to lowercase and then you can get the index .