Cümle oluşturan sözcükleri ve kelimelerin pozitiosnlarını kullanarak bir senetence yazmayı denemeye çalışıyorum. Kodumu çalıştırdığımda 'List dizini aralık dışı' hatası alıyorum. Neyin yanlış olduğunu anlayamıyorum. Her türlü yardıma çok minnettar olurum. Teşekkürler :). Bütün kodlarım aşağıda. In) sizin kompres fonksiyonunda EğerPython'da cümlenin yeniden oluşturulması: Liste endeksi aralık dışı
2 tsk3wds.txt kelimeleri yazarken bir dosya words.txt aramaya
1) Tekrar Oluştur fonksiyonunda:
def compress(): #function that will compress the inputted sentence/sentences
sentence = input("Input the sentence that you wish to be compressed") #Sentence to be compressed
sentence.lower()#Puts sentence in lower-case
sentencelist = sentence.split() #Splits the sentence into a list
d = {} #Dictionary
plist = [] #List that contains the positions of the words
wds = []
for i in sentencelist: #iterating through the inputted sentence
if i not in wds: #if item not in list of words...
wds.append(i) #...append to the list of words
for i ,j in enumerate(sentencelist):#Enumerates the sentence and gets the positions
if j in (d): #if the item (j) is in the d
plist.append(d[j]) #append the item to the list of positions
else:
d[j] =i #else, append item (i) to position list
plist.append(i) #appends to the list of positions
print (plist) #print the list containing the positions.
with open ("tsk3pos.txt", "wt") as txt: #opens the file
position_string = " ".join(str(x) for x in plist) #makes/recreates sentence using positons and words
txt.write(position_string)
txt.close() #closes the file
with open ("tsk3wds.txt", "wt") as txt: #opens the file
for item in wds: #iterates through list of words
txt.write("%s\n" % item) #puts lists in the file
txt.close() #closes the file
print (wds) #prints list that contains words that are in the sentence
main() #calls main function
def recreate(compress): #function that will be used to recreate the compressed sentence.
num = [] #creates list for positions (blank)
wds = [] #creates list for words (blank)
with open("words.txt", "r") as txt: #with statement opening the word text file
for line in txt: #iterating over each line in the text file.
wds += line.split() #turning the textfile into a list and appending it to num
with open("tsk3pos.txt", "r") as txt: #opens text file with list of positions in read code
for line in txt: #iterates through
num += [int(i) for i in line.split()] #turns the textfile into list and appends to blank list
recreate = ' '.join(wds[pos] for pos in num) #makes/recreates sentence using positons and words
with open("recreate.txt", "wt") as txt: #opens recreate text file in write mode
txt.write(recreate) #writes sentences to 'recreate' text file
main() #calls the main function
def main(): #defines main function
print("Do you want to compress an input or recreate a compressed input?") #user input
user = input("Type 'a' if you want to compress an input. Type 'b' if you wan to recreate an input").lower() #gives user choice ad puts input in lower case
if user not in ("a","b"): #if input isn't a or b...
print ("That's not an option. Please try again") #give error message
elif user == "a": #if input is a...
compress() #...call the compress function
elif user == "b": #if input is b...
recreate(compress) #...call recreate function with compress as argument
main() #calls main function
main() #Calls main function
Hangi satırda hata alıyorsunuz? Bize yığın izini göster ;-) – Alfe
@Alfe Yanıt verdiğiniz için teşekkür ederiz. 74, 72, 69, 36, 71 ve 54. satırlarda hatalar alıyorum. – rmce
Lütfen tüm hataların ayrıntılarını belirtin. –