2013-09-26 4 views
0

Ищете идеи о том, почему мой код не пишет в новый текстовый файл. Нет никаких ошибок, чтобы дать мне ключ.Почему мой код не записывается в новый текстовый файл

def writeFile (filename, text): 
file = open(greenBottle.txt, 'w') 
file.write(text) 
file.close() 

    def main (text): 

     big_nums = ['no','One','Two','Three','Four','Five','Six','Seven','Eight','Nine','Ten'] 
     text_one = (' green bottles \nHanging on the wall\n') 
     small_nums = [' no',' one',' two',' three',' four',' five',' six',' seven',' eight',' nine',' ten'] 
     text_two = ('And if one green bottle \nShould accidentally fall\nThere\'ll be') 
     text_three = (' green bottles \nHanging on the wall\n \n') 
     result=[] 
     text=new_string 
     new_string='' 

     for i in range(10, 0, -1): 
      result.append(big_nums[i] + str(text_one)) 
      result.append(big_nums[i] + str(text_one)) 
      result.append(text_two + small_nums[i-1] + text_three) 
      return result('') 
      print(''.join(main(text))) 

    if __name__ == '__main__': 
     writeFile('greenBottle.txt',text) 


    main(text) 
+0

Какая операционная система? возможно, у вас нет права на запись в эту папку? – alfasin

+0

Win 7 и я админ, так что все разрешения – user2715061

+3

Это не может быть код, который вы используете, если у вас нет ошибок. Это имеет много из них, например. text = new_string перед объявлением new_string, return result (''), когда результатом является список (поэтому не вызываемый), текст не определяется при вызове writeFile ('greenBottle.txt', text) ... Покажите нам ваш точный код , – miles82

ответ

-1

Насколько я понимаю эти строки глючат и должны быть удалены и вы не можете return из для цикла, но только из функции:

return result('') 
print(''.join(main(text))) 

и эта линия повторяется:

result.append(big_nums[i] + str(text_one)) 

После того как я переписал код:

def writeFile (filename, text): 
    with open(filename,'w') as file: 
     for line in text: 
      file.write(line) 

def main(): 
    result=[] 
    big_nums = ['no','One','Two','Three','Four','Five','Six','Seven','Eight','Nine','Ten'] 
    text_one = (' green bottles \nHanging on the wall\n') 
    small_nums = [' no',' one',' two',' three',' four',' five',' six',' seven',' eight',' nine',' ten'] 
    text_two = ('And if one green bottle \nShould accidentally fall\nThere\'ll be') 
    text_three = (' green bottles \nHanging on the wall\n \n') 
    new_string='' 
    text = new_string 

    for i in range(0,10): 
     result.append(big_nums[i] + str(text_one)) 
     #result.append(big_nums[i] + str(text_one)) 
     result.append(text_two + small_nums[i-1] + text_three) 
     #return result('') 
     #print(''.join(main(text))) 

    writeFile('greenBottle.txt',result) 

main() 

Выход:

no green bottles 
Hanging on the wall 
And if one green bottle 
Should accidentally fall 
There'll be ten green bottles 
Hanging on the wall 

One green bottles 
Hanging on the wall 
And if one green bottle 
Should accidentally fall 
There'll be no green bottles 
Hanging on the wall 

Two green bottles 
Hanging on the wall 
And if one green bottle 
Should accidentally fall 
There'll be one green bottles 
Hanging on the wall 

Three green bottles 
Hanging on the wall 
And if one green bottle 
Should accidentally fall 
There'll be two green bottles 
Hanging on the wall 

Four green bottles 
Hanging on the wall 
And if one green bottle 
Should accidentally fall 
There'll be three green bottles 
Hanging on the wall 

Five green bottles 
Hanging on the wall 
And if one green bottle 
Should accidentally fall 
There'll be four green bottles 
Hanging on the wall 

Six green bottles 
Hanging on the wall 
And if one green bottle 
Should accidentally fall 
There'll be five green bottles 
Hanging on the wall 

Seven green bottles 
Hanging on the wall 
And if one green bottle 
Should accidentally fall 
There'll be six green bottles 
Hanging on the wall 

Eight green bottles 
Hanging on the wall 
And if one green bottle 
Should accidentally fall 
There'll be seven green bottles 
Hanging on the wall 

Nine green bottles 
Hanging on the wall 
And if one green bottle 
Should accidentally fall 
There'll be eight green bottles 
Hanging on the wall 
+0

@http: //stackoverflow.com/users/2425215/k-dawg Спасибо, сэр. Не знал, что вы не можете вернуться из цикла for, но только из функции. но, поскольку я сказал, что я только начинающий, спасибо за ваши знания. – user2715061

+0

Его цикл 'for' находится внутри функции, поэтому, конечно, он может вернуться от него. – abarnert

Смежные вопросы