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.