[Tutor] Please Help

2013-03-21 Thread Arijit Ukil
I am new to python. I like to calculate average of the numbers by reading 
the file 'digi_2.txt'. I have written the following code:

def average(s): return sum(s) * 1.0 / len(s)

f = open ("digi_2.txt", "r+")

list_of_lists1 = f.readlines()


for index in range(len(list_of_lists1)):
 

tt = list_of_lists1[index]

print 'Current value :', tt

avg =average (tt)


This gives an error:

def average(s): return sum(s) * 1.0 / len(s)
TypeError: unsupported operand type(s) for +: 'int' and 'str'

I also attach the file i am reading.



Please help to rectify.

Regards,
Arijit Ukil
Tata Consultancy Services
Mailto: arijit.u...@tcs.com
Website: http://www.tcs.com

Experience certainty.   IT Services
Business Solutions
Outsourcing




From:
Alan Gauld 
To:
tutor@python.org
Date:
03/21/2013 06:00 AM
Subject:
Re: [Tutor] Help
Sent by:
"Tutor" 



On 20/03/13 19:57, travis jeanfrancois wrote:
> I create a function that allows the user to a create sentence by
>   inputing  a string and to end the sentence with a  period meaning
> inputing "." .The problem is while keeps overwriting the previuos input

'While' does not do any such thing. Your code is doing that all by 
itself. What while does is repeat your code until a condition
becomes false or you explicitly break out of the loop.

> Here is my code:
>
> def B1():

Try to give your functions names that describe what they do.
B1() is meaningless, readSentence() would be better.


>   period = "."
>   # The variable period is assigned

Its normal programming practice to put the comment above
the code not after it. Also comments should indicate why you
are doing something not what you are doing - we can see that
from the code.

>   first = input("Enter the first word in your sentence ")
>   next1 = input("Enter the next word in you sentence or enter period:")

>   # I need store the value so when while overwrites next1 with the next
> input the previous input is stored and will print output when I call it
> later along with last one
>   # I believe the solution is some how implenting this expression x = x+
> variable

You could be right. Addition works for strings as well as numbers.
Although there are other (better) options but you may not have covered 
them in your class yet.

>   while  next1 != (period) :

You don;t need the parentheses around period.
Also nextWord might be a better name than next1.
Saving 3 characters of typing is not usually worthwhile.

>  next1  = input("Enter the next word in you sentence or enter 
period:")

Right, here you are overwriting next1. It's not the while's
fault - it is just repeating your code. It is you who are
overwriting the variable.

Notice that you are not using the first that you captured?
Maybe you should add next1 to first at some point? Then you
can safely overwrite next1 as much as you like?

>  if next1 == (period):

Again you don;t need the parentheses around period

>  next1 = next1 + period

Here, you add the period to next1 which the 'if' has
already established is now a period.

>  print ("Your sentence is:",first,next1,period)

And now you print out the first word plus next1 (= 2 periods) plus a 
period = 3 periods in total... preceded by the phrase "Your sentence 
is:" This tells us that the sample output you posted is not from this 
program... Always match the program and the output when debugging or you 
will be led seriously astray!

> PS : The" #"  is I just type so I can understand what each line does

The # is a comment marker. Comments are a very powerful tool that 
programmers use to explain to themselves and other programmers
why they have done what they have.

When trying to debug faults like this it is often worthwhile
grabbing a pen and drawing a chart of your variables and
their values after each time round the loop.
In this case it would have looked like

iterationperiod  first   next1
0.   I   am
1.   I   a
2.   I   novice
3.   I   ..

If you aren't sure of the values insert a print statement
and get the program to tell you, but working it out in
your head is more likely to show you the error.


HTH,

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/l

[Tutor] Please Help

2013-03-22 Thread Arijit Ukil
Hi,

I have another small problem. Pls help.

I have written the following code:

f = open ("digi_2.txt", "r+")
lines = f.readlines()
for line in lines:
number_list = []
for number in line.split(','):
number_list.append(float(number))

s_data = []
for i in range(len(number_list)):
if number_list[i] > 5:
s_data = number_list[i]

print 'Data val:', s_data


The problem is: it is printing only the last value, not all the values. In 
this case '10', not '9,8,6,10'.



Regards,
Arijit Ukil
Tata Consultancy Services
Mailto: arijit.u...@tcs.com
Website: http://www.tcs.com

Experience certainty.   IT Services
Business Solutions
Outsourcing
________



From:
Amit Saha 
To:
Arijit Ukil 
Cc:
tutor@python.org
Date:
03/21/2013 05:30 PM
Subject:
Re: [Tutor] Please Help



Hi Arijit,

On Thu, Mar 21, 2013 at 8:42 PM, Arijit Ukil  wrote:
>
> I am new to python. I like to calculate average of the numbers by 
reading
> the file 'digi_2.txt'. I have written the following code:
>
> def average(s): return sum(s) * 1.0 / len(s)
>
> f = open ("digi_2.txt", "r+")
>
> list_of_lists1 = f.readlines()
>
>
> for index in range(len(list_of_lists1)):
>
>
> tt = list_of_lists1[index]
>
> print 'Current value :', tt
>
> avg =average (tt)
>
>
> This gives an error:
>
> def average(s): return sum(s) * 1.0 / len(s)
> TypeError: unsupported operand type(s) for +: 'int' and 'str'
>
> I also attach the file i am reading.
>
>
>
> Please help to rectify.

The main issue here is that when you are reading from a file, to
Python, its all strings. And although, 'abc' + 'def' is valid, 'abc' +
5 isn't (for example). Hence, besides the fact that your average
calculation is not right, you will have to 'convert' the string to an
integer/float to do any arithmetic operation on them. (If you know C,
this is similar to typecasting). So, coming back to your program, I
will first demonstrate you a few things and then you can write the
program yourself.

If you were to break down this program into simple steps, they would be:

1. Read the lines from a file (Assume a generic case, where you have
more than one line in the file, and you have to calculate the average
for each such row)
2. Create a list of floating point numbers for each of those lines
3. And call your average function on each of these lists

You could of course do 2 & 3 together, so you create the list and call
the average function.

So, here is step 1:

with open('digi.txt','r') as f:
lines = f.readlines()

Please refer to
http://docs.python.org/2/tutorial/inputoutput.html#methods-of-file-objects
for an explanation of the advantage of using 'with'.

Now, you have *all* the lines of the file in 'lines'. Now, you want to
perform step 2 for each line in this file. Here you go:

for line in lines:
number_list = []
for number in line.split(','):
number_list.append(float(number))

 (To learn more about Python lists, see
http://effbot.org/zone/python-list.htm). It is certainly possible to
use the index of an element to access elements from a list, but this
is more Pythonic way of doing it. To understand this better, in the
variable 'line', you will have a list of numbers on a single line. For
example: 1350696461, 448.0, 538660.0, 1350696466, 448.0. Note how they
are separated by a ',' ? To get each element, we use the split( )
function, which returns a list of the individual numbers. (See:
http://docs.python.org/2/library/stdtypes.html#str.split). And then,
we use the .append() method to create the list. Now, you have a
number_list which is a list of floating point numbers for each line.

Now, step 2 & 3 combined:

for line in lines:
number_list = []
for number in line.split(','):
number_list.append(float(number))
print average(number_list)

Where average( ) is defined as:

def average(num_list):
return sum(num_list)/len(num_list)



There may be a number of unknown things I may have talked about, but i
hope the links will help you learn more and write your program now.

Good Luck.
-Amit.


--
http://amitsaha.github.com/


=-=-=
Notice: The information contained in this e-mail
message and/or attachments to it may contain 
confidential or privileged information. If you are 
not the intended recipient, any dissemination, use, 
review, distribution, printing or copying of the 
information contained in this e-mail message 
and/or attachments to it are strictly prohibited. If 
you have received this communication in error, 
please notify us by reply e-mail or telephone and 
immediately and permanently delete the message 
and any attachments. Thank you


1,0,9,0,8,6,0,10___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Please Help

2013-03-22 Thread Arijit Ukil
I have the following data points.
data = [1,2,0,9,0,1,4]
I like to store in an array and print the odd-indexed points, i.e. 2, 9,1 
(considering index starts at 0)

I have written the following code which is not running:

import math

number_list = [1,2,0,9,0,1,4]

number_list_1 = []
for k in range(math.floor(float(len(number_list)/2))):
if (k< math.floor(float(len(number_list)/2))):
number_list_1[k] = number_list[k*2 + 1]

print 'data: ', number_list_1 

Please help

Regards,
Arijit Ukil
Tata Consultancy Services
Mailto: arijit.u...@tcs.com
Website: http://www.tcs.com

Experience certainty.   IT Services
Business Solutions
Outsourcing

=-=-=
Notice: The information contained in this e-mail
message and/or attachments to it may contain 
confidential or privileged information. If you are 
not the intended recipient, any dissemination, use, 
review, distribution, printing or copying of the 
information contained in this e-mail message 
and/or attachments to it are strictly prohibited. If 
you have received this communication in error, 
please notify us by reply e-mail or telephone and 
immediately and permanently delete the message 
and any attachments. Thank you


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Help required

2013-04-04 Thread Arijit Ukil
I am new to python. My intention is to read the file digi.txt and store in 
separate arrays all the values of each columns. However, the following 
program prints only the last value, i.e. 1350696500.0.
Please help to rectify this.

f = open ("digi.txt", "r+")

datafile = f.readlines()

list_of_lists = datafile
for data in list_of_lists:
lstval = data.split (',')
timest = float(lstval[0])
energy = float(lstval[1])

f.close()
print timest

Regards,
Arijit Ukil
Tata Consultancy Services
Mailto: arijit.u...@tcs.com
Website: http://www.tcs.com

Experience certainty.   IT Services
Business Solutions
Outsourcing

=-=-=
Notice: The information contained in this e-mail
message and/or attachments to it may contain 
confidential or privileged information. If you are 
not the intended recipient, any dissemination, use, 
review, distribution, printing or copying of the 
information contained in this e-mail message 
and/or attachments to it are strictly prohibited. If 
you have received this communication in error, 
please notify us by reply e-mail or telephone and 
immediately and permanently delete the message 
and any attachments. Thank you


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] running python from windows command prompt

2013-04-10 Thread Arijit Ukil
I like to run a python program "my_python.py" from windows command prompt. 
This program ( a function called testing) takes input as block data (say 
data = [1,2,3,4] and outputs processed single data.

import math

def avrg(data):
return sum(data)/len(data)

def testing (data):
val = avrg(data)
out = pow(val,2)
return out

Pls help.

Regards,
Arijit Ukil
Tata Consultancy Services
Mailto: arijit.u...@tcs.com
Website: http://www.tcs.com

Experience certainty.   IT Services
Business Solutions
Outsourcing

=-=-=
Notice: The information contained in this e-mail
message and/or attachments to it may contain 
confidential or privileged information. If you are 
not the intended recipient, any dissemination, use, 
review, distribution, printing or copying of the 
information contained in this e-mail message 
and/or attachments to it are strictly prohibited. If 
you have received this communication in error, 
please notify us by reply e-mail or telephone and 
immediately and permanently delete the message 
and any attachments. Thank you


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Running python from windows command prompt

2013-04-10 Thread Arijit Ukil
I like to run a python program "my_python.py" from windows command prompt. 
This program ( a function called testing) takes input as block data (say 
data = [1,2,3,4] and outputs processed single data. 

import math 

def avrg(data): 
return sum(data)/len(data) 

def testing (data): 
val = avrg(data) 
out = pow(val,2) 
return out 

I am using python 2.6. My intention is to run from windows command prompt 
as

python my_python.py 1 3  2

However I am getting error: No such file in the directory, Errno 21

Pls help.

Regards,
Arijit Ukil
Tata Consultancy Services
Mailto: arijit.u...@tcs.com
Website: http://www.tcs.com

Experience certainty.   IT Services
Business Solutions
Outsourcing

=-=-=
Notice: The information contained in this e-mail
message and/or attachments to it may contain 
confidential or privileged information. If you are 
not the intended recipient, any dissemination, use, 
review, distribution, printing or copying of the 
information contained in this e-mail message 
and/or attachments to it are strictly prohibited. If 
you have received this communication in error, 
please notify us by reply e-mail or telephone and 
immediately and permanently delete the message 
and any attachments. Thank you


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Running python from windows command prompt

2013-04-10 Thread Arijit Ukil
Thanks for the help. Now I have modifed the code as:

import sys

def main(argv):

data = int(sys.argv[1])
 
avg = average (data)
 
print "Average:", avg

def average(num_list):
return sum(num_list)/len(num_list)

if __name__ == "__main__":
   main(sys.argv[1:])

When running in command line I am getting these errors:
In case 1. data = sys.argv[1]
In case 2, data = float(sys.argv[1])
In case 3, data = int (sys.argv[1])





Regards,
Arijit Ukil
Tata Consultancy Services
Mailto: arijit.u...@tcs.com
Website: http://www.tcs.com

Experience certainty.   IT Services
Business Solutions
Outsourcing




From:
Alan Gauld 
To:
tutor@python.org
Date:
04/10/2013 10:58 PM
Subject:
Re: [Tutor] Running python from windows command prompt
Sent by:
"Tutor" 



On 10/04/13 13:32, Arijit Ukil wrote:
> I like to run a python program "my_python.py" from windows command
> prompt. This program ( a function called testing) takes input as block
> data (say data = [1,2,3,4] and outputs processed single data.
>

Hopefully the code below is not your entire program. If it is it won't 
work. You define 2 functions but never call them.

Also you don't print anything so there is no visible output.
Finally this a code does not read the values from sys.argv.
(See my tutorial topic 'Talking to the user')

As for the error message, others have replied but basically you need
to either change to the folder that your file exists in or specify
the full path at the command prompt.

> import math
>
> def avrg(data):
> return sum(data)/len(data)
>
> def testing (data):
> val = avrg(data)
> out = pow(val,2)
> return out

HTH
-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


=-=-=
Notice: The information contained in this e-mail
message and/or attachments to it may contain 
confidential or privileged information. If you are 
not the intended recipient, any dissemination, use, 
review, distribution, printing or copying of the 
information contained in this e-mail message 
and/or attachments to it are strictly prohibited. If 
you have received this communication in error, 
please notify us by reply e-mail or telephone and 
immediately and permanently delete the message 
and any attachments. Thank you


<>___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] sha-256 without using hashlib

2013-04-26 Thread Arijit Ukil
I like to implement sha-256 without using implement. It is easy using 
hashlib as:

import hashlib
m = hashlib.sha256()
m.update("hi")
print m.hexdigest()

If anybody has pointer on sha-256 implemented without using hashlib 
library, please share.

Regards,
Arijit Ukil
Tata Consultancy Services
Mailto: arijit.u...@tcs.com
Website: http://www.tcs.com

Experience certainty.   IT Services
Business Solutions
Outsourcing

=-=-=
Notice: The information contained in this e-mail
message and/or attachments to it may contain 
confidential or privileged information. If you are 
not the intended recipient, any dissemination, use, 
review, distribution, printing or copying of the 
information contained in this e-mail message 
and/or attachments to it are strictly prohibited. If 
you have received this communication in error, 
please notify us by reply e-mail or telephone and 
immediately and permanently delete the message 
and any attachments. Thank you


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Regarding python function arguments

2013-06-11 Thread Arijit Ukil
i am writing following python function:

def my_func (arg1, arg2, arg3):

however, I am not always going to pass all the arguments. sometimes only 
arg1 is passed, sometimes arg1 and arg2 are passed; sometimes arg1, arg2, 
arg3 are passed.
How can i manage this?

Regards,
Arijit Ukil
Tata Consultancy Services
Mailto: arijit.u...@tcs.com
Website: http://www.tcs.com

Experience certainty.   IT Services
Business Solutions
Consulting

=-=-=
Notice: The information contained in this e-mail
message and/or attachments to it may contain 
confidential or privileged information. If you are 
not the intended recipient, any dissemination, use, 
review, distribution, printing or copying of the 
information contained in this e-mail message 
and/or attachments to it are strictly prohibited. If 
you have received this communication in error, 
please notify us by reply e-mail or telephone and 
immediately and permanently delete the message 
and any attachments. Thank you


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Data persistence problem

2013-06-20 Thread Arijit Ukil
I have following random number generation function

def rand_int ():
rand_num = int(math.ceil (random.random()*1000))
return rand_num

I like to make the value of rand_num (return of rand_int) static/ 
unchanged after first call even if it is called multiple times. If x= 
rand_int () returns 45 at the first call, x should retain 45 even in 
multiple calls.
Pls help.

Regards,
Arijit Ukil
Tata Consultancy Services
Mailto: arijit.u...@tcs.com
Website: http://www.tcs.com

Experience certainty.   IT Services
Business Solutions
Consulting

=-=-=
Notice: The information contained in this e-mail
message and/or attachments to it may contain 
confidential or privileged information. If you are 
not the intended recipient, any dissemination, use, 
review, distribution, printing or copying of the 
information contained in this e-mail message 
and/or attachments to it are strictly prohibited. If 
you have received this communication in error, 
please notify us by reply e-mail or telephone and 
immediately and permanently delete the message 
and any attachments. Thank you


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor