0

Guys, I have two questions. At first programme, I do not know where is mistake.

subor = open("slova.txt","r") obsah = subor.read() subor.close() pocet_slov = len(obsah.split()) pocet_znakov = len(obsah) zoznam_slov = obsah.split() najdlhsie_slovo = max(zoznam_slov, key=len) poradie_najdlhsieho_slova = zoznam_slov.index(najdlhsie_slovo) + 1 print("Pocet slov: ", pocet_slov) print("Pocet znakov: ", pocet_znakov) print("Najdlhsie slovo: ", najdlhsie_slovo) print("Poradie najdlhsieho slova: ", poradie_najdlhsieho_slova)

15th Feb 2023, 6:28 PM
Matej
Matej - avatar
3 Answers
+ 9
Matej > we need to know what the content of the file *slova.txt* is. > we also need a task description to know what the program is going to achieve.
15th Feb 2023, 6:50 PM
Lothar
Lothar - avatar
+ 2
What problem or error you can see there? What is your task? edit: if the file exist, then it works fine...
15th Feb 2023, 6:38 PM
Jayakrishna 🇼🇳
+ 1
MoĆŸno preklad nie je takĂœ dobrĂœ, ale mali by ste skontrolovaĆ„, či je sĂșbor v rovnakom adresĂĄri ako vĂĄĆĄ python sĂșbor #najjednoduchĆĄĂ­ spĂŽsob, ako to urobiĆ„: with open('slova.txt','r')as subor: slova = subor.read() subor.close() # zoznam slov bez znaku `\n` zoznam_slov = [slovo for slovo in slova.split()if slovo!='\n'] pocet_znakov = len(slova) pocet_slov = len(zoznam_slov) najdlhsie_slovo = max(zoznam_slov,key=len) poradie_najdlhsich = zoznam_slov.index(najdlhsie_slovo) print('Zoznam slov: ',zoznam_slov) print('Počet znakov: ',pocet_znakov) print('Počet slov: ',pocet_slov) print('NajdlhĆĄie slovo: ',najdlhsie_slovo) print('Index toho: ',poradie_najdlhsich)
15th Feb 2023, 6:59 PM
Ervis Meta
Ervis Meta - avatar