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)
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.
+ 2
What problem or error you can see there?
What is your task?
edit: if the file exist, then it works fine...
+ 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)