First program using functions

I am learning Python, and this book requires me to "Make a program using a function", so I learned a bit and tried to use as much as I know so far to make a program with the following characteristics:

  • Create a Python program that opens and reads this file.
  • Introduces itself to the user and gives the name Script
  • Is there a math problem using the -Sum 2 function of a number
  • Creates a new text file and copies the results of the function in it using the function
  • Opens a text file and reads it inside the program, then closes it.

So, everything is correct, googled a lot of material to make it work and some learned. Script creates a new text file, but I'm having trouble copying the value obtained by the function into it using file.write (res) .

Here is the code:

from sys import argv

script, file_1 = argv
txt = open(file_1)

def average_values (n1, n2): 
    res = (n1 + n2)
    return res

print "Hello my name is Lenard or, as my creator called me, %s. \nYou have been instructed to do this:\n" % script
print txt.read()
print "The results will be copied to a new Text document but they will also be printed at the end of the program, thanks for your preference!"

n1 = raw_input("Introduce Number 1, please: ")
n2 = raw_input("Introduce Number 2, please: ")

res = average_values(n1, n2)

fname = 'Results.txt'
file_sample = open(fname, 'w')
file_sample.write(res)
print "Here are the results:..." 
print res
print file_sample.read()

When I run it, I get the following error: `` res not specified ''. I know that it is very simple for some of you, but I tried something, and I just can not get it to write the value received by the function ... So, any ideas?

I am very sorry that I need to ask about this and get others to do my work, but I tried to solve the problem, I just do not know what to do, any conclusions will be appreciated.

Thank you for the attention.

EDIT 3

. N1 N2 (, 5 5) n1 n2. : 55 10. txt.

+4
3

, ( :)) , .

#!/usr/bin/env python

from sys import argv

def average_values(n1, n2): 
    res = 0.5 * (n1 + n2)
    return res


script, file_1 = argv

print "Hello my name is Lenard or, as my creator called me, %s. \
You have been instructed to do this:\n" % script

txt = open(file_1)
print txt.read()
txt.close()    

print "The results will be copied to a new Text document but they will also \
be printed at the end of the program, thanks for your preference!"
n1 = int(raw_input("Introduce Number 1, please: "))
n2 = int(raw_input("Introduce Number 2, please: "))

res = average_values(n1, n2)

fname = 'Results.txt'
file_sample = open(fname, 'w')
file_sample.write(str(res))
file_sample.close()

print "Here are the results:..." 
print res

file_sample = open(fname, 'r')
print file_sample.read()
file_sample.close()

average_values() , .

, write(), .

, . , , , .

- , , , , , .


Edit

, , "Results.txt" , . ,

file_sample.write(str(res))

file_sample.write("%f\n" % res)

, , .

+2

, 2 :

1: raw_input :

n1 = float(raw_input("Introduce Number 1, please: "))
n2 = float(raw_input("Introduce Number 2, please: "))

Python - , .

def average_values (n1, n2): 
    res = (n1+ n2)
    return res

n1 n2 . , "+" . , n1 = 5 n2 = 5, "55" beacuse raw_input instied 5, . 5s "55".

2:

 def average_values (n1, n2): 
        res = float(n1) + float(n2)
        return res
+3

- , !

, , - :

def average_students (n1, n2): 
    res = (float(n1) + float(n2)) / 2
    print "The result is %f" %res
    return res

n1 = raw_input("Introduce Number 1, please: ")
n2 = raw_input("Introduce Number 2, please: ")

res = average_students(n1,n2)

# print intro stuff here if needed

fname = 'Results.txt'
output = open(fname, 'w')
output.write(res)
output.close()

print "Write completed, wrote:"
print res

, average_students , , +

, , , . PM 2Ring, .

+2

Source: https://habr.com/ru/post/1568431/


All Articles