среда, 22 апреля 2015 г.

Проблема с записью в файл и печатью на экран / The problem with writing to a file and printing it to the screen

Всем привет,  сегодня я озадачился одной проблемой, у меня есть вот такой отрывок кода:
/
Hi everyone, today I've faced the same problem, I have this piece of code:


if r_value==1:
        r_addition = r_digit + r_digit2
        file_calculation.write  ('{0} + {1}' .format( r_digit, r_digit2))
        file_calculation.write (str(' = %s\n' % r_addition))


В током виде как я написал выше - все работает великолепно, но мне кажется так как то не правильно - не красиво. Сейчас я покажу как я хотел сделать:
/
In the current form as I wrote above - it works great, but I think it as something wrong, it is not beautiful. Now I will show how I wanted to do:



if r_value==1:
        r_addition = r_digit + r_digit2
        file_calculation.write  ('{0} + {1} = {3}' .format( r_digit, r_digit2, r_addition))

В таком виде как я написал выше не работает и выдает синтаксическую ошибку с которой я еще не разобрался. Сами понимаете что такая форма записи на много удобнее и приятнее чем то, что я написал выше.
/
In this form, as I wrote above does not work and throws a syntax error which I haven't figured out yet. You know that this form of entry is much easier and more enjoyable than what I wrote above.

Ошибка:
/
Error:
IndexError: tuple index out of range

понедельник, 20 апреля 2015 г.

Вывод из файла / Input from file

Доброе утро, я давно не писал ничего так как был приступ лени, выходные выбили меня из колеи и мне тяжело было вернуться.

Сегодня я дописал вывод информации из файла к калькулятору из предыдущей статьи (Калькулятор),  сам по себе вывод очень прост, сначала делаем открытие файла на чтение после с помощью for считываем строки из файла и печатаем на экран.


Если вы меня читаете напишите пожалуйста в комментариях, что еще добавить к калькулятору ??


/


Good morning, I have not written anything since had an attack of laziness, weekend knocked me off balance and it was hard to go back.

Today I finished the withdrawal of the information from the file to the calculator from the previous article (Calculator), by itself, the conclusion is very simple, first make opening the file for reading after using for read lines from file and print to the screen.

If you read me please write in the comments what else to add to the calculator ??




# -*- coding: utf-8 -*- #Выставляем кодировку / Set the encoding

FILE_NAME = 'data.txt'  #Присваиваем переменной название файла с результатами /
                        #Assign to the variable the name of the results file
def printfile():
    file_print = open (FILE_NAME,'r') #Открываем на чтение / Opened for reading
    for line in file_print:   #Считываем строки из файла / Read lines from file
        print (line)   #Печатаем на экран / Printed on the screen
    file_print.close() #Закрываем сессию / Close the session