How to do for loop on line contains strings? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5

How to do for loop on line contains strings?

I have file contains 1000 lines of names with numbering like : 1 Andrew Mai 2 Mina Nada 3 Khaled Menna I want to search for the name that the user will input it and say its position. so i want to do for loop on the line that has the name to be able to know the position of name later.

4th Oct 2018, 1:49 PM
Mona Mohamed
Mona Mohamed - avatar
12 Answers
+ 1
Hello mona, sorry I wasn't responding any sooner. Anyways, I noticed a little problem from the use of string find() method for finding names, not that it was bad, but it was tricky to tell whether the name found was that of a boy, or a girl. There's also another problem with the search accuracy, for example, if we search for "Daniel" find() method will return a match for "Danielle", "Daniels", "Daniela" or any name that contains "Daniel". This can render the program not to behave as expected. So, based on that I wrote a side version based on your code, but I use the extraction operator that splits each line into three strings, (rank, boy name, and girl name). I found it rather easier to tell whether the match on the line was a boy's name or a girl's. And the search accuracy is also improved that a search for "Daniel" no longer match "Danielle" or "Daniela". See my review here and tell me what you think. https://code.sololearn.com/cb3264105te7/?ref=app
6th Oct 2018, 9:38 AM
Ipang
+ 3
For example, if the user enters the name “Justice,” then the program should output: Justice is ranked 519 in popularity among boys. Justice is ranked 518 in popularity among girls. If the user enters the name “Walter,” then the program should output: Walter is ranked 376 in popularity among boys. Walter is not ranked among the top 1000 girl names.
5th Oct 2018, 10:50 AM
Mona Mohamed
Mona Mohamed - avatar
+ 2
Can you explain why you need to use for loop in this case? as I see your example of file content, I see the number and the name is separated by a whitespace, so I think you can use find() method from string class to find the whitespace and read the substring following the whitespace position. Do you have a code in progress in attempt to solve the case? you should attach the code URL in original post if you do, so others can understand what you want and help you.
5th Oct 2018, 6:08 AM
Ipang
+ 2
mona I think there's a problem with your code, I can't even open it. And you must mention the restrictions as well because I see some examples online employs different rules what is allowed to use or not.
5th Oct 2018, 9:25 AM
Ipang
+ 2
The text file babynames2012.txt, which is available online from the book’s Web site, contains a list of the 1000 most popular boy and girl names in the United States for the year 2012 as compiled by the Social Security Administration. This is a space-delimited file of 1000 entries in which the rank is listed first, followed by the corresponding boy name and girl name. The most popular names are listed first and the least popular names are listed last. For example, the file begins with 1 Jacob Sophia 2 Mason Emma 3 Ethan Isabella This indicates that Jacob is the most popular boy name and Sophia is the most popular girl name. Mason is the second most popular boy name and Emma is the second most popular girl name. Write a program that allows the user to input a name. The program should then read from the file and search for a matching name among the girls and boys. If a match is found, it should output the rank of the name.The program should also indicate if there is no match.
5th Oct 2018, 10:49 AM
Mona Mohamed
Mona Mohamed - avatar
+ 2
mona I have seen the problem on the net, I understand what it means, but unfortunately I still can't open your code, can you check if you can open that code link?
5th Oct 2018, 12:24 PM
Ipang
5th Oct 2018, 1:07 PM
Mona Mohamed
Mona Mohamed - avatar
+ 1
I want to know if the name that the user input it belong to boy's or girl's names. the name of file: (babynames2012.txt) you can get it online. there is my code: https://code.sololearn.com/cyXSbetCkfMb/?ref=app
5th Oct 2018, 8:40 AM
Mona Mohamed
Mona Mohamed - avatar
+ 1
Ipang, Thank you very much for helping me, you write the code exactly as i want, but can you explain me what "string rank" do in your code, it is the first time i here it (i don't know it).
7th Oct 2018, 7:30 AM
Mona Mohamed
Mona Mohamed - avatar
+ 1
Hello mona, again sorry for late reply, the "rank" in that line is just a string variable, just like "boyName" and "girlName". The while loop initiates and continue as long as there is a line to be read and extracted into variable "rank", "boyName" and "girlName". For example look at the following line, from the file: 10 Christopher Elizabeth "10" will be stored in "rank" "Christopher" will be stored in "boyName" "Elizabeth" will be stored in "girlName" Come to think of this, I guess "rank" can be used to replace the functionality of "lineNumber" variable. The "rank" will still contain the correct rank of the names in case there are lines deleted from the file, but "lineNumber" will yield differently because there are some lines deleted.
7th Oct 2018, 2:41 PM
Ipang
+ 1
Ipang, Thank you very much for your great effort. now i understand the code very well.
7th Oct 2018, 8:46 PM
Mona Mohamed
Mona Mohamed - avatar
0
mona You're welcome, keep moving on : )
8th Oct 2018, 5:00 AM
Ipang