Re: [Tutor] Trying to access a random value in a list

2012-01-12 Thread Nick W
first problem: easy fix just remember that len() returns the actual
number of items in the list but that list is indexed starting at 0 so
just replace your line of
pick = len(names)
with:
   pick = len(names) - 1
and for problem #2:
just use string formating... like for example instead of:
print (names[win_number], " is the winner!")
try something along the lines of:
   print("{} is a winner".format(names[win_number]))
HTH
Pacific Morrowind

On 1/12/12, Claude Matherne  wrote:
> Hello all,
> I am a beginner to python and I am trying to make a simple program that
> takes in some names as input into a list and then randomly picks a vaule
> from that list as a winner.
>
> Here is the code:
>
> import random
>
> choice = None
>
> names = [ ]
>
> while choice != "0":
> print(
> """
> 0 - Exit
> 1 - Enter a name
> 2 - Pick a winner
> """)
> choice = input("Choice: ")
> print()
>
> if choice == "0":
> print ("Goodbye.")
>
> elif choice == "1":
> name = input("Please enter a name: ")
> names.append(name)
> print (names, "have been entered.")
>
> elif choice == "2":
> pick = len(names)
> win_number =random.randint(0,pick)
> print (names[win_number], " is the winner!")
>
> input("/n/nPress enter to exit.")
>
>
> First problem, sometimes I get a error where the value is out of range.
> Problem becuase of the way I set up the random value.
> Second problem, but not a big one, is when I print the lists of names as
> they are entered, I get quotations around the name.
>
> I'm sur I am over thinking this, but any help would be great.
>
> Thanks
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Set Reply-To field to Tutor@python.org

2013-01-29 Thread Nick W
My personal opinion (with whatever limited weight that has on this list
since I've only answered a few questions - and probably half of them I've
accidentally only sent to the op)/how I read it is that RFC 2822 actually
allows lists to set reply-to header; by my view the list software is
forwarding to everyone else and therefor counts as the most recent
sender/author. I admit that that is a somewhat different conclusion to
others that I've read as to the meaning of 2822, but that seems logical to
me and also my personal preference is for having the reply-to header be set
to the list address.
Nick


On Tue, Jan 29, 2013 at 7:44 AM, Albert-Jan Roskam  wrote:

>
>
> >
> > To summarize existing opinions on this matter:
> >
> > http://marc.merlins.org/netrants/listreplyto.html
> >
> > You might want to familiarize yourself with existing literature on the
> > matter before starting a new flame war.
>
> Hmmm... False alarm?
>
> Page blocked
>
> The page you've been trying to access was blocked.
> Reason: Access Denied! The requested URL is a Spyware site.
> Transaction ID is 5107F01CFF920603D57F.
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] operator order

2013-01-31 Thread Nick W
because python process the expression on the right side of the assignment
first.
ie d *= 3+4 basically is the equivalent of writing (2) * (3+4).
Hope that explains it.
Nick


On Thu, Jan 31, 2013 at 10:36 AM, heathen  wrote:

> why is this:
>
> >>> d = 2
> >>> d *= 3 + 4
> >>> d
> 14
>
> not this:
>
> >>> d = 2
> >>> d = d * 3 + 4
> >>> d
> 10
>
> __**_
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/**mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Read from large text file, parse, find string, print string + line number to second text file.

2013-02-01 Thread Nick W
quite easy to do; just use enumerate - as so:
myString = "The String"
with open('largeFile', 'r') as inF:
for index, line in enumerate(inF):
#myString = "The String" ##Not here because otherwise this gets run
for every single line of the large file (which is nasty waste of resources)
if 'myString' in line:
with open(thenewfile', 'w') as f:
f.write("Line #%d has string: %s"  (index, line))
That will print the whole line into the new file, If you only want the
characters before that use find and take a slice of the line instead.
HTH
Nick


On Fri, Feb 1, 2013 at 12:09 PM, Scurvy Scott  wrote:

> Hey all how're things?
>
> I'm hoping for some guidance on a problem I'm trying to work through.
> I know this has been previously covered on this list but I'm hoping it
> won't bother you guys to run through it again.
>
> My basic program I'm attempting to create is like this..
>
> I want to read from a large, very large file.
> I want to find a certain string
> if it finds the string I would like to select the first 15-20
> characters pre and proceeding the string and then output that new
> string to a new file along with the line the string was located on
> within the file.
>
> It seems fairly straight forward but I'm wondering if y'all can point
> me to a direction that would help me accomplish this..
>
> Firstly I know I can read a file and search for the string with (a
> portion of this code was found on stackoverflow and is not mine and
> some of it is my own)
>
> with open('largeFile', 'r') as inF:
> for line in inF:
> myString = "The String"
> if 'myString' in line:
> f = open(thenewfile', 'w')
> f.write(myString)
> f.close()
>
> I guess what I'm looking for then is tips on A)My stated goal of also
> writing the 15-20 characters before and after myString to the new file
> and
> B)finding the line number and writing that to the file as well.
>
> Any information you can give me or pointers would be awesome, thanks in
> advance.
>
> I'm on Ubuntu 12.10 running LXDE and working with Python 2.7
>
> Scott
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Read from large text file, parse, find string, print string + line number to second text file.

2013-02-02 Thread Nick W
I'd suggest having the newfile open after outfile is defined also a close
statement on newfile - or use it with 'with' such as:

... and replace the last line like so:
with open(outfile, 'w') as newfile:
main(mystring, infile, newfile)

(and looking muchly improved, well done)
Nick


On Fri, Feb 1, 2013 at 8:57 PM, Scurvy Scott  wrote:

> And just for the records sake, this is what I've gotten and you guys
> should see obviously that you helped a lot and I learned a thing or
> two so I won't have to ask the same silly questions next time:
>
>
>
>
> def main(mystring, infile, outfile):
> with open('infile', 'r') as inF:
> for index, line in enumerate(inF):
> if myString in line:
> newfile.write("string %s found on line
> #%d" (line, index))
> print "complete."
>
>
> if __name__ == '__main__':
>import sys
>newfile = open('outfile', 'w')
>help_text = "usage: python scanfile.py STRINGTOSEARCH
> IMPORTFILENAME OUTPUTFILENAME"
>if '-h' in sys.argv or '--help' in sys.argv or len(sys.argv) == 0:
>print (help_text)
>sys.exit()
>myString = sys.argv[1]
>infile = sys.argv[2]
>outfile = sys.argv[3]
>main(mystring, infile, outfile)
>
> Look right to you? Looks okay to me, except maybe the three ORs in the
> information line, is there a more pythonic way to accomplish that
> task?
>
> Scott
>
> On Fri, Feb 1, 2013 at 8:31 PM, Scurvy Scott  wrote:
> >> Best practice is to check if your program is being run as a script
> before
> >> doing anything. That way you can still import the module for testing or
> >> similar:
> >>
> >>
> >> def main(mystring, infile, outfile):
> >># do stuff here
> >>
> >>
> >> if __name__ == '__main__':
> >># Running as a script.
> >>import sys
> >>mystring = sys.argv[1]
> >>infile = sys.argv[2]
> >>outfile = sys.argv[3]
> >>main(mystring, infile, outfile)
> >>
> >>
> >>
> >> Best practice for scripts (not just Python scripts, but *any* script)
> is to
> >> provide help when asked. Insert this after the "import sys" line,
> before you
> >> start processing:
> >>
> >>if '-h' in sys.argv or '--help' in sys.argv:
> >>print(help_text)
> >>sys.exit()
> >>
> >>
> >>
> >> If your argument processing is more complicated that above, you should
> use
> >> one of the three argument parsing modules that Python provides:
> >>
> >> http://docs.python.org/2/library/getopt.html
> >> http://docs.python.org/2/library/optparse.html (deprecated -- do not
> use
> >> this for new code)
> >> http://docs.python.org/2/library/argparse.html
> >>
> >>
> >> getopt is (in my opinion) the simplest to get started, but the weakest.
> >>
> >> There are also third-party argument parsers that you could use. Here's
> one
> >> which I have never used but am intrigued by:
> >>
> >> http://docopt.org/
> >>
> >>
> >>
> >> --
> >> Steven
> >>
> >> ___
> >> Tutor maillist  -  Tutor@python.org
> >> To unsubscribe or change subscription options:
> >> http://mail.python.org/mailman/listinfo/tutor
> >
> > Steve-
> >  thanks a lot for showing me the if __name__ = main part
> > I've often wondered how it was used and it didn't make sense until I
> > saw it in my own code if that makes any sense.
> > Also appreciate the help on the "instructional" side of things.
> >
> > One question related to the instruction aspect- does this make sense to
> you?
> >
> > If len(sys.argv) == 0:
> > print "usage: etc etc etc"
> >
> >
> >
> > Nick, Dave, and Steve, again, you guys are awesome. Thanks for all your
> help.
> >
> > Scott
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor