Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

First program using Functions

Tags:

python

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

  1. Create a Python program that Opens and Reads this file.
  2. Introduces itself to the user and gives the Script name
  3. Does a math problem using a function -Sum 2 numbers
  4. Creates a new text file and copies the results of the function in it using a function
  5. Opens the text file and reads it inside the program then closes it.

So everything is right, googled lots of stuff to make it work and learned some. The script does create a new textfile but I am having problems 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 is not specified''. I know that this is very simple for some of you, but I tried some stuff and I just cant get it to writte the value obtained by the function... So any ideas?

I really hate to have to ask this and make others do my work, but I did try to solve the problem, I just dont know what to do, will appreciate any leads.

Thanks for your attention.

EDIT 3

Now I manage to create the file. However the values of N1 and N2 (lets say 5 and 5) and instead of summing them n1 and n2 are printed. I get this result: 55 instead of 10. Same result is written in the new txt document.

like image 716
Little Foot Avatar asked Mar 18 '23 13:03

Little Foot


1 Answers

Here's a slightly modified version of your program that (I think :) ) does what you want.

#!/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()

I've modified your average_values() function so that it does actually calculate the average of its arguments.

I've also changed the input statements so that they return integers instead of strings, and changed the write() call, so that its argument is a string.

I've also closed files when appropriate. In particular, you need to close a file you open for writing before you read from it.

There's still some room for improvement with this code, so please don't treat it a perfect example, but I hope it will get you heading down the right track.


Edit

I guess I ought to mention that 'Results.txt' will only contain the result, it won't end with a newline. You can change that by changing

file_sample.write(str(res))

to

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

although you should probably learn about the more modern techniques of string formatting.

like image 108
PM 2Ring Avatar answered Mar 29 '23 12:03

PM 2Ring