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 ответов
+ 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)