[Tutor] Please Help

Arijit Ukil arijit.ukil at tcs.com
Thu Mar 21 11:42:05 CET 2013


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.ukil at tcs.com
Website: http://www.tcs.com
____________________________________________
Experience certainty.   IT Services
                        Business Solutions
                        Outsourcing
____________________________________________



From:
Alan Gauld <alan.gauld at btinternet.com>
To:
tutor at python.org
Date:
03/21/2013 06:00 AM
Subject:
Re: [Tutor] Help
Sent by:
"Tutor" <tutor-bounces+arijit.ukil=tcs.com at python.org>



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

iteration                period          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 at 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


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20130321/dc17085f/attachment.html>
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: digi_2.txt
URL: <http://mail.python.org/pipermail/tutor/attachments/20130321/dc17085f/attachment.txt>


More information about the Tutor mailing list