Newbie, homework help, please.

Ian Kelly ian.g.kelly at gmail.com
Sat Apr 21 14:03:17 EDT 2012


On Sat, Apr 21, 2012 at 11:28 AM, someone <wesbroom at gmail.com> wrote:
> for item in userinput:
>    openfile=open(textfile,'w');openfile.writelines("%s\n" % item for item in userinput);openfile.close()

The for loop here means that the file will be written and rewritten
four times.  The end result is the same, but you only need to write it
once.

>    stringlength=len(string)

This line assigns an int to stringlength.

>    stringlength=stringlength("%s\n" % item for item in stringlength) + 2 * (3 + 3)

And this line attempts to both iterate over stringlength and call it,
either of which will fail since ints can't be iterated over or called.

What you need to do here is split the SHI into a list of individual
lines using the str.split method, or by reading it that way in the
first place using indat.readlines instead of indat.read.  Then
determine the length of the longest line using the max function or a
for loop.

>    hBorder=stringlength//2*"* "+"*"[:stringlength%2]
>    spacer="*"+" "*(stringlength - 2)+"*"
>    fancyText="*  "+string+"  *"

Since there are multiple lines, you will need to do the "* " + line +
" *" once for each line to do the left and right borders, not once for
the entire string.  Otherwise you will end up with just one * on the
left and one * on the right.



More information about the Python-list mailing list