Guys, I have a question. In this programme, I do not know where is mistake. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

Guys, I have a question. In this programme, I do not know where is mistake.

Task is: List of names. We need to process the export of employee names from the company's information system. The list of employees in the text file contains firstly only first names (one first name in each line - it can be multi-word) and next lines contain only surnames (the order of first and last names is preserved). Create a program that: (a) reads the names from the file, finds and lists the number of names in the file b) find and list the longest first name (multiple first names are considered as one long first name) c) find and list the longest surname d) create a result file with a list of employees (serial number, first name and last name) and write it to the file My program: subor = open('mena.txt', 'r') name = [] for line in subor: meno.append(row.strip()) number_of_names = len(name) print("Number of names in the file:", pocet_menus) longest_name = max(name, key=len) print("The longest name is:", longest_name) lastname = [] for line in subor: lastname.append(row.strip()) longest_lastname = max(lastname, key=en) print("The longest last name is:", longest_last_name) result = open('mena.txt', 'w') for i in range(number_of_names): subor_result.write(str(i+1) + ". " + firstname[i] + " " + lastname[i] + "\n") result.close() subor.close() It always writes to me max() arg is an empty sequence. This is my text files: Anna Fero Dita Adam Jana Great Small Medium High Small

1st Mar 2023, 5:06 PM
Matej
Matej - avatar
10 Answers
+ 9
Matej , we don't need to read the file twice. just read it, as already done with the first try. the result is a list of strings with all names, the length is 10. after having done this, you can separate first name and surname for further purpose.
1st Mar 2023, 6:50 PM
Lothar
Lothar - avatar
+ 7
santosh patel , since you have posted your question in an already existing feed, people may not find it. it would be better to srart a new feed. > concerning your question: there are various choices of *programming languages* that serve different needs. you can go for creating websites, or learn coding to create apps or you can go for data science and much more... > give us a bit more information so you will get recommendations that will fit your needs.
6th Mar 2023, 10:21 AM
Lothar
Lothar - avatar
+ 5
meno = [] for riadok in subor: meno.append(riadok.strip()) subor.close() pocet_mien = len(meno) print("Počet mien v súbore:", pocet_mien) najdlhsie_meno = max(meno[:pocet_mien//2], key=len) print("Najdlhšie meno je:", najdlhsie_meno) priezviska = meno[pocet_mien//2:] najdlhsie_priezvisko = max(priezviska, key=len) print("Najdlhšie priezvisko je:", najdlhsie_priezvisko) vysledok = open('mena2.txt', 'w') for i in range(pocet_mien//2): vysledok.write(str(i+1) + ". " + meno[i] + " " + priezviska[i] + "\n") vysledok.close() print() ovladanie = open('mena2.txt', 'r') print(ovladanie.read()) ovladanie.close()
1st Mar 2023, 8:28 PM
JaScript
JaScript - avatar
+ 3
Matej I found the problem. The code tries to read the file a second time. But after reading it the first time, the read pointer is at the end of the file, so it has nothing more to read. To fix it, reset the read pointer back to the beginning of the file before the second loop. You can do this by inserting the line: subor.seek(0) Also, I found yet another inconsistent name betwixt vysledok and subor_vysledok in the last loop.
1st Mar 2023, 6:14 PM
Brian
Brian - avatar
+ 2
I think I see a typo in the call to the max() function. Change key=en to key=len.
1st Mar 2023, 5:20 PM
Brian
Brian - avatar
+ 2
The variable name is inconsistent betwixt longest_lastname and longest_last_name.
1st Mar 2023, 5:29 PM
Brian
Brian - avatar
+ 1
That's just me I copied wrong. It keeps giving me this error.
1st Mar 2023, 5:26 PM
Matej
Matej - avatar
+ 1
Ok, I copied one more time. subor = open('mena.txt', 'r') meno = [] for riadok in subor: meno.append(riadok.strip()) pocet_mien = len(meno) print("Počet mien v súbore:", pocet_mien) najdlhsie_meno = max(meno, key=len) print("Najdlhšie meno je:", najdlhsie_meno) priezviska = [] for riadok in subor: priezviska.append(riadok.strip()) najdlhsie_priezvisko = max(priezviska, key=len) print("Najdlhšie priezvisko je:", najdlhsie_priezvisko) vysledok = open('mena.txt', 'w') for i in range(pocet_mien): subor_vysledok.write(str(i+1) + ". " + meno[i] + " " + priezviska[i] + "\n") vysledok.close() subor.close()
1st Mar 2023, 5:44 PM
Matej
Matej - avatar
0
Hi
3rd Mar 2023, 9:25 AM
santosh patel
santosh patel - avatar
0
Any help me how to start
3rd Mar 2023, 9:25 AM
santosh patel
santosh patel - avatar