+ 2

Python3 : Beginner - Loops

Hi - first of all, excuse my english : i'm french ! And also a beginner in python3 I have an exercise : 1 - make a list of people (first name + last name) 2 - ask for a random letter 3 - if the random letter equals the first letter of the first name, the guy wins. And it can be two or more winners 1 - Here's my list : studentList = [ "Edmond Prochain", "Sam Démange", "Sandra Nicouette", "Jordy Nateur", "Emmy Sphère", ] 2 - here's how I call the random letter : import string string.ascii_uppercase import random my_letter = random.choice(string.ascii_uppercase) 3 ... that's the most important part and I'm confused ... I was considering doing a "for" + "if" but i can't find how to code : "if the random letter equals the first letter of the first name : return the list of the winner(s)" I'm not asking for all the solution, just some help Thanks a lot !

25th Nov 2019, 6:09 PM
Lain
2 Respuestas
+ 3
There's a function for strings i.e .startswith() which you can use. Below code searches in complete list for random character. for name in studentList: if name.startswith(my_letter): print(name)
25th Nov 2019, 6:58 PM
blACk sh4d0w
blACk sh4d0w - avatar
+ 1
Thank you so much !!!!
25th Nov 2019, 7:48 PM
Lain