Re: [Tutor] Running a loop

2011-10-16 Thread Peter Otten
Jason Barry wrote:

> I am using Windows 7 and python 3.1. This is a block from a slot machine
> code. It takes the random generated words and indicates if it wins or
> loses. I can't figure out why it wants to print the last print statement
> 'Loser' no matter if the elif statements are true.
> 
> import random
> 
> wheel1=['ZOMBIE', 'WITCH', 'CAT', 'GHOST',
> 'CANDY','PUMPKIN','PUMPKIN','CANDY', 'GHOST','CANDY']
> 
> wheel2=['ZOMBIE', 'WITCH', 'CAT', 'GHOST',
> 'CANDY','PUMPKIN','PUMPKIN','CANDY', 'GHOST','CANDY']
> 
> wheel3=['ZOMBIE', 'WITCH', 'CAT', 'GHOST',
> 'CANDY','PUMPKIN','PUMPKIN','CANDY', 'GHOST','CANDY']

The wheels are all the same and you don't plan to modify them; therefore you 
can use a single wheel variable for all wheels in the slot machine

> wheel1index=''

No need to initialise the variable with a dummy value when you proceed to 
see the real value immediately afterwards.

> wheel1index=wheel1[random.randint(0, len(wheel1) - 1)]
> wheel2index=wheel2[random.randint(0, len(wheel2) - 1)]
> wheel3index=wheel3[random.randint(0, len(wheel3) - 1)]

Have a look at random.choice() which has the same effect. Also, consider 
putting the results into a list as in

chosen = [random.choice(wheel) for dummy in range(3)]

> winning=0

Unused variable.

> def checkwin (wheel1index, wheel2index, wheel3index):

You can shorten this function considerably with the help of sets and dicts. 
A complete example script:

import random
from collections import Counter

wheel = ['zombie', 'witch', 'cat', 'ghost', 
'candy','pumpkin','pumpkin','candy', 'ghost','candy']

_plurals = {
"witch": "witches",
"candy": "candies"}

def plural(word):
return _plurals.get(word, word + "s")

def checkwin (chosen):
chosen_dict = Counter(chosen)
if len(chosen_dict) == 1:
print("wins {}.".format(plural(chosen[0])))
elif chosen_dict == {"witch": 2, "cat": 1}:
print("wins witches and cat")
elif chosen_dict == {"pumpkin": 2, "ghost": 1}:
print("wins pumpkins and ghost")
elif chosen_dict.keys() == {"candy", "pumpkin", "ghost"}:
print("wins pumpkin, ghost, and candy.")
else:
print("Loser")

for i in range(1000):
chosen = [random.choice(wheel) for wheel_index in range(3)]
print(" ".join(chosen).ljust(25), end=" --> ")
checkwin(chosen)


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


Re: [Tutor] Python Job Scheduling package

2011-10-16 Thread Rafael Durán Castañeda
And what about celery???
2011/10/16 Steven D'Aprano 

> bod...@googlemail.com wrote:
>
>> Have you thought about writing your own? Others have posted some
>> useful links, but in all honesty you could hack something together to
>> achieve that in next to no time
>>
>
> Anyone can "hack something together" in next to no time, but getting a
> quality package that is well-written, thoroughly tested and debugged may
> take a bit longer.
>
>
> --
> Steven
>
> __**_
> 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


[Tutor] Socket and Ports

2011-10-16 Thread Jacob Bender
Dear Tutors,

 I've been having an issue with socket. I wanted to use it for
transmitting strings over the Internet. The problem is that my friend
insists that allowing python to transmit and receive information via an
Internet port is a bad idea. He claimed that I could(and probably would)
receive information that wouldn't necessarily do my computer any good(in a
nutshell).
 First of all, I thought that you could specify where the message was
supposed to come from via an IP address, while you were in the process of
creating your clients(or server). That way, I shouldn't be receiving all
kinds of random stuff from people on the same port that don't have that IP
address.
 My friend also claims that hackers could use my python distribution to
hack other programs on my system(all without my permission, of course). And
ultimately steal information or bring down my system. All via socket,
because I'd allow only that program to access the port.
 In conclusion, basically my friend says that whatever I do, hackers can
and probably will somehow take control of my computer in some way, shape, or
form. I, however think this to be untrue because I'm specifying where the
information is supposed to come from and I'm not going to open my entire
computer to the port, I'm ONLY allowing python to receive and transmit
information. Please help. Thanks,

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


[Tutor] Bounded Linear Search

2011-10-16 Thread toganm
Hi,
I am trying to learn programming and python and one of the books I use has 
an exercise for "Bounded Linear Search algorithm" under the heading in 
Accumulating Unique Values at


I have coded the exercises as below and what I can not figure it out is the 
code works along as spin[0] and spine[-1] are not same but when it is the 
code does not work. What am I missing ?


spin=[18, 10, 2, 19, 20, 10, 2, 7, 26, 10,2,2,18]

uniq=[]

for v in spin:
i=0
uniq.append(v)
while uniq[i]!=v:
i+=1
if uniq[i]==v:
if i!=len(uniq)-1:
   uniq.pop(-1)

print(uniq)


Thanks

Togan

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


Re: [Tutor] Socket and Ports

2011-10-16 Thread bob gailer

On 10/16/2011 8:28 AM, Jacob Bender wrote:

Dear Tutors,

 I've been having an issue with socket. I wanted to use it for 
transmitting strings over the Internet.


That's good, because strings is all you can transmit.

The problem is that my friend insists that allowing python to transmit 
and receive information via an Internet port is a bad idea. He claimed 
that I could(and probably would) receive information that wouldn't 
necessarily do my computer any good(in a nutshell).


I am not the expert on this issue. My view:

once you establish a socket connection then you wait to receive data. 
All the socket software (Python or other) does is receive a string. What 
you do with it is up to you. If you apply eval or exec to it than 
anything could happen. No one can IMHO cause any action via socket.


[snip]

--
Bob Gailer
919-636-4239
Chapel Hill NC

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


Re: [Tutor] Bounded Linear Search

2011-10-16 Thread Peter Otten
tog...@users.sourceforge.net wrote:

> Hi,
> I am trying to learn programming and python and one of the books I use has
> an exercise for "Bounded Linear Search algorithm" under the heading in
> Accumulating Unique Values at
> 
 exercises>
> 
> I have coded the exercises as below and what I can not figure it out is
> the code works along as spin[0] and spine[-1] are not same but when it is
> the code does not work. What am I missing ?
> 
> 
> spin=[18, 10, 2, 19, 20, 10, 2, 7, 26, 10,2,2,18]
> 
> uniq=[]
> 
> for v in spin:
> i=0
> uniq.append(v)
> while uniq[i]!=v:
> i+=1
> if uniq[i]==v:
> if i!=len(uniq)-1:
>uniq.pop(-1)
> 
> print(uniq)

Consider the simple example spin = [42, 42]

First iteration of the for loop:
v = 42
i = 0
uniq = [42]
while uniq[0] != 42: # False, while loop will not be entered
...

Second iteration of the for loop:

v = 42
i = 0
uniq = [42, 42]
while uniq[0] != 42 # False, while loop while not be entered

The fix is to move the if ... check out of the while loop

for v in spin:
i = 0
uniq.append(v)
while uniq[i] != v:
i += 1
# at this point uniq[i] == v is guaranteed, so we don't need the outer 
if
if i != len(uniq) - 1:
uniq.pop(-1) # or: del uniq[-1]

First iteration of the for loop:
v = 42
i = 0
uniq = [42]
while uniq[0] != v: # False, while loop will not be entered
if 0 != len([42]) - 1 # 0 != 0, False, last item of uniq will not be removed

Second iteration of the for loop:
v = 42
i = 0
uniq = [42, 42]
while uniq[0] != 42: # False, while loop will not be entered
if 0 != len([42, 42]) -1: # 0 != 1, True, last item of uniq will be removed

To verify that the algorithm is correct now you could walk through 
increasingly more complex sample data, which may be possible in this case, 
but rarely ever for an entire script. Instead the common approach is to pick 
a few samples along with the expected outcomes, feed them to your function 
and verify that they give the expected output

def unique_values(items):
   ...
   return uniq

assert unique_values([42, 42]) == [42]
assert unique_values([1, 2, 3, 2]) == [1, 2, 3]
...

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


Re: [Tutor] Socket and Ports

2011-10-16 Thread Hugo Arts
On Sun, Oct 16, 2011 at 4:20 PM, bob gailer  wrote:
> On 10/16/2011 8:28 AM, Jacob Bender wrote:
>>
>> Dear Tutors,
>>
>>     I've been having an issue with socket. I wanted to use it for
>> transmitting strings over the Internet.
>
> That's good, because strings is all you can transmit.
>
>> The problem is that my friend insists that allowing python to transmit and
>> receive information via an Internet port is a bad idea. He claimed that I
>> could(and probably would) receive information that wouldn't necessarily do
>> my computer any good(in a nutshell).
>
> I am not the expert on this issue. My view:
>
> once you establish a socket connection then you wait to receive data. All
> the socket software (Python or other) does is receive a string. What you do
> with it is up to you. If you apply eval or exec to it than anything could
> happen. No one can IMHO cause any action via socket.
>

vulnerabilities in the lower level stack notwithstanding, of course.
But in essence, using sockets in python is not any more dangerous than
using sockets in any other language. You have to watch what you're
doing and be careful with the data you receive, but as long as you do
that you shouldn't be in any danger.

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


Re: [Tutor] Bounded Linear Search

2011-10-16 Thread toganm
Peter Otten wrote:

> To verify that the algorithm is correct now you could walk through
> increasingly more complex sample data, which may be possible in this case,
> but rarely ever for an entire script. Instead the common approach is to
> pick a few samples along with the expected outcomes, feed them to your
> function and verify that they give the expected output
> 
> def unique_values(items):
>...
>return uniq
> 
> assert unique_values([42, 42]) == [42]
> assert unique_values([1, 2, 3, 2]) == [1, 2, 3]

Thanks for the tip and where I was failing to see

Togan

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


Re: [Tutor] Can I set LD_LIBRARY_PATH within a .py file?

2011-10-16 Thread Albert-Jan Roskam
Hi Hugo,
 
You are absolutely right. Thank you! It took me a lot of reading and tinkering 
to find out that typing the following in the terminal works:
export LD_LIBRARY_PATH=\path\to\the\lib
python main.py # contains import to my program + calls to the functions in it.
 
I find it strange though, that ctypes.CDLL() does not accept library names 
*with the full path*. In Linux, you could do it, but it seems that all the 
dependencies of the libary in that non-standard location are looked for ONLY in 
that non-standard location.

Cheers!!
Albert-Jan


~~
All right, but apart from the sanitation, the medicine, education, wine, public 
order, irrigation, roads, a fresh water system, and public health, what have 
the Romans ever done for us?
~~

From: Hugo Arts 
>To: Albert-Jan Roskam 
>Cc: Python Mailing List 
>Sent: Sunday, October 16, 2011 1:22 AM
>Subject: Re: [Tutor] Can I set LD_LIBRARY_PATH within a .py file?
>
>On Sat, Oct 15, 2011 at 9:51 PM, Albert-Jan Roskam  wrote:
>> Hello,
>> Can I set the LD_LIBRARY_PATH environment variable (on-the-fly) within a .py
>> file?
>> I would like to use an .so-file that lives in a non-standard location.
>>
>> This does not work:
>> try:
>>    os.environ["LD_LIBRARY_PATH"]  += (":" + path)
>> except KeyError:
>>    os.environ["LD_LIBRARY_PATH"] = path
>> Currently, I can only run the program in the terminal:
>> export LD_LIBRARY_PATH=/home/dude/Desktop/test
>> python /home/dude/Desktop/testLoadLibLinux.py
>> This works (yaaayy!), but I'd like to run the .py file directly.
>> Is this possible? I also don't  like the fact that I can't test the .py file
>> in Idle.
>> Perhaps a complicating factor is a bug in LD_LIBRARY_PATH
>> in Linux Ubuntu 10 (the version I'm using):
>> https://bugs.edge.launchpad.net/ubuntu/+source/xorg/+bug/366728
>> https://bugs.launchpad.net/ubuntu/+source/xorg/+bug/366728/comments/21
>> solution:
>> sudo gedit /etc/X11/Xsession.options
>> (change "use-ssh-agent" into "no-use-ssh-agent")
>>
>> Thank you in advance for your thoughts!
>>
>> Cheers!!
>> Albert-Jan
>>
>
>Alright, I'm not going to pretend to be an expert on this one, a bit
>of this is speculation and inference from what I know about dynamic
>linking. In short, I don't think you can modify LD_LIBRARY_PATH on the
>fly and have it actually work. The reason for this is that the linker
>runs and finds all the libraries *before* the python process actually
>starts. So by the time you go and modify the environment, all
>libraries have already been linked, and your modified variable is
>never even read by the linker.
>
>So the best you can do is write a tiny wrapper to set LD_LIBRARY_PATH
>and then run your actual script through it. Or you could set the
>environment variable and then fork(), I suppose, since the child will
>inherit the modified environment. But the wrapper is your simplest
>option.
>
>HTH,
>Hugo
>
>
>___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Socket and Ports

2011-10-16 Thread Jacob Bender
Thank you, and I'm not planning on executing any data I receive from
anybody. So I should be pretty safe...

On Sun, Oct 16, 2011 at 10:48 AM, Hugo Arts  wrote:

> On Sun, Oct 16, 2011 at 4:20 PM, bob gailer  wrote:
> > On 10/16/2011 8:28 AM, Jacob Bender wrote:
> >>
> >> Dear Tutors,
> >>
> >> I've been having an issue with socket. I wanted to use it for
> >> transmitting strings over the Internet.
> >
> > That's good, because strings is all you can transmit.
> >
> >> The problem is that my friend insists that allowing python to transmit
> and
> >> receive information via an Internet port is a bad idea. He claimed that
> I
> >> could(and probably would) receive information that wouldn't necessarily
> do
> >> my computer any good(in a nutshell).
> >
> > I am not the expert on this issue. My view:
> >
> > once you establish a socket connection then you wait to receive data. All
> > the socket software (Python or other) does is receive a string. What you
> do
> > with it is up to you. If you apply eval or exec to it than anything could
> > happen. No one can IMHO cause any action via socket.
> >
>
> vulnerabilities in the lower level stack notwithstanding, of course.
> But in essence, using sockets in python is not any more dangerous than
> using sockets in any other language. You have to watch what you're
> doing and be careful with the data you receive, but as long as you do
> that you shouldn't be in any danger.
>
> Hugo
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] 6 random numbers

2011-10-16 Thread ADRIAN KELLY

hello all,
anyone know how i would go about printing 6 random numbers, i know i could copy 
and paste 6 times (which would work) but i was thinking about a while loop, ie. 
while lottery_numbers.count is <7. 
Is it possible to code this? is it possible to count random variables? i am 
trying to keep the program as simple as possible, cheers

any help would be welcome, 

import random
lottery_numbers=random.randrange(1,42)
print lottery_numbers
  
  ___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Tutor Digest, Vol 92, Issue 77

2011-10-16 Thread Max S.
k you in advance for your thoughts!
> >>
> >> Cheers!!
> >> Albert-Jan
> >>
> >
> >Alright, I'm not going to pretend to be an expert on this one, a bit
> >of this is speculation and inference from what I know about dynamic
> >linking. In short, I don't think you can modify LD_LIBRARY_PATH on the
> >fly and have it actually work. The reason for this is that the linker
> >runs and finds all the libraries *before* the python process actually
> >starts. So by the time you go and modify the environment, all
> >libraries have already been linked, and your modified variable is
> >never even read by the linker.
> >
> >So the best you can do is write a tiny wrapper to set LD_LIBRARY_PATH
> >and then run your actual script through it. Or you could set the
> >environment variable and then fork(), I suppose, since the child will
> >inherit the modified environment. But the wrapper is your simplest
> >option.
> >
> >HTH,
> >Hugo
> >
> >
> >
> -- next part --
> An HTML attachment was scrubbed...
> URL: <
> http://mail.python.org/pipermail/tutor/attachments/20111016/792dc422/attachment-0001.html
> >
>
> --
>
> Message: 3
> Date: Sun, 16 Oct 2011 15:00:04 -0400
> From: Jacob Bender 
> To: Hugo Arts 
> Cc: Python Tutor , bob gailer 
> Subject: Re: [Tutor] Socket and Ports
> Message-ID:
> >
> Content-Type: text/plain; charset="iso-8859-1"
>
> Thank you, and I'm not planning on executing any data I receive from
> anybody. So I should be pretty safe...
>
> On Sun, Oct 16, 2011 at 10:48 AM, Hugo Arts  wrote:
>
> > On Sun, Oct 16, 2011 at 4:20 PM, bob gailer  wrote:
> > > On 10/16/2011 8:28 AM, Jacob Bender wrote:
> > >>
> > >> Dear Tutors,
> > >>
> > >> I've been having an issue with socket. I wanted to use it for
> > >> transmitting strings over the Internet.
> > >
> > > That's good, because strings is all you can transmit.
> > >
> > >> The problem is that my friend insists that allowing python to transmit
> > and
> > >> receive information via an Internet port is a bad idea. He claimed
> that
> > I
> > >> could(and probably would) receive information that wouldn't
> necessarily
> > do
> > >> my computer any good(in a nutshell).
> > >
> > > I am not the expert on this issue. My view:
> > >
> > > once you establish a socket connection then you wait to receive data.
> All
> > > the socket software (Python or other) does is receive a string. What
> you
> > do
> > > with it is up to you. If you apply eval or exec to it than anything
> could
> > > happen. No one can IMHO cause any action via socket.
> > >
> >
> > vulnerabilities in the lower level stack notwithstanding, of course.
> > But in essence, using sockets in python is not any more dangerous than
> > using sockets in any other language. You have to watch what you're
> > doing and be careful with the data you receive, but as long as you do
> > that you shouldn't be in any danger.
> >
> > Hugo
> >
> -- next part --
> An HTML attachment was scrubbed...
> URL: <
> http://mail.python.org/pipermail/tutor/attachments/20111016/a2d911bb/attachment-0001.html
> >
>
> --
>
> >Message: 4
> >Date: Sun, 16 Oct 2011 19:43:06 +
> >From: ADRIAN KELLY 
> >To: 
> >Subject: [Tutor] 6 random numbers
> >Message-ID: 
> >Content-Type: text/plain; charset="iso-8859-1"
> >
> >
> >hello all,
> >anyone know how i would go about printing 6 random numbers, i know i could
> copy and paste 6 times (which would work) but i was >thinking about a while
> loop, ie. while lottery_numbers.count is <7.
> >Is it possible to code this? is it possible to count random variables? i
> am trying to keep the program as simple as possible, cheers
> >
> >any help would be welcome,
> >
> >import random
> >lottery_numbers=random.randrange(1,42)
> >print lottery_numbers
>

In fact it is.  I notice, however, that you include 2 arguments in the
randrange method, which only takes 1 argument.  To do this, this code should
work:

times = 1
while times < 7:
import random
lottery_numbers=random.randrange(1, 42)
print lottery_numbers
times+=1


>
>
> -- next part --
> An HTML attachment was scrubbed...
> URL: <
> http://mail.python.org/pipermail/tutor/attachments/20111016/4c9e4d5c/attachment.html
> >
>
> --
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
> End of Tutor Digest, Vol 92, Issue 77
> *
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] 6 random numbers

2011-10-16 Thread शंतनू
On 17-Oct-2011, at 1:13 AM, ADRIAN KELLY wrote:

> hello all,
> anyone know how i would go about printing 6 random numbers, i know i could 
> copy and paste 6 times (which would work) but i was thinking about a while 
> loop, ie. while lottery_numbers.count is <7. 
> Is it possible to code this? is it possible to count random variables? i am 
> trying to keep the program as simple as possible, cheers
> 
> any help would be welcome, 
> 
> import random
> lottery_numbers=random.randrange(1,42)
> print lottery_numbers
>   

Following example may be useful:
x = [random.randrange(1, 1+random.randrange(42) for _ in range(100)]

Useful read: 
http://docs.python.org/tutorial/datastructures.html

-- 
shantanoo
http://xkcd.com/221/

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


[Tutor] httplib2.RelativeURIError: Only absolute URIs are allowed. uri

2011-10-16 Thread pierre dagenais

Hi,
I'm running python 3.2 on Ubuntu 11.04 and have a problem with httplib2. 
I'm not sure if it's my ignorance or a bug in the code.


This code works:

#! /usr/bin/python3.2

import urllib.request

sock = urllib.request.urlopen("file:///home/pierre/bookmarks.html")
htmlSource = sock.read()
sock.close()
print (htmlSource)


and so does this one:

#! /usr/bin/python3.2

import httplib2

h = httplib2.Http('.cache')
response, content = h.request("http://google.com";)
print(response)


but this one doesn't:

#! /usr/bin/python3.2

import httplib2

h = httplib2.Http('.cache')
response, content = h.request("file:///home/pierre/bookmarks.html")
print(response)

I get this error message when I run it:

Traceback (most recent call last):
  File "Meteo5.py", line 6, in 
response, content = h.request("file:///home/pierre/bookmarks.html")
  File "/usr/lib/python3/dist-packages/httplib2/__init__.py", line 984, 
in request

(scheme, authority, request_uri, defrag_uri) = urlnorm(uri)
  File "/usr/lib/python3/dist-packages/httplib2/__init__.py", line 137, 
in urlnorm
raise RelativeURIError("Only absolute URIs are allowed. uri = %s" % 
uri)
httplib2.RelativeURIError: Only absolute URIs are allowed. uri = 
file:///home/pierre/bookmarks.html



Isn't "file:///home/pierre/bookmarks.html" a valid URI? It works fine 
with urllib, why not with httplib2?


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


Re: [Tutor] Randrange [was: Re: Tutor Digest, Vol 92, Issue 77]

2011-10-16 Thread Alan Gauld

On 16/10/11 21:10, Max S. wrote:



On Sun, Oct 16, 2011 at 3:44 PM, mailto:tutor-requ...@python.org>> wrote:

Send Tutor mailing list submissions to
tutor@python.org 


When replying, please edit your Subject line so it is more specific
than "Re: Contents of Tutor digest..."


Please follow the instructions.
And while you are at it please don't send the entire content of the 
digest message, edit it down to the bit that is relevant, it's much 
easier for us to read it that way, and saves bandwidth for those paying 
by the byte!



 >
 >import random
 >lottery_numbers=random.randrange(1,42)
 >print lottery_numbers

In fact it is.  I notice, however, that you include 2 arguments in the
randrange method, which only takes 1 argument.



Nope.


>>> help(random.randrange)
Help on method randrange in module random:

randrange(self, start, stop=None, step=1, int=, 
default=None, maxwidth=9007199254740992) method of random.Random instance

Choose a random item from range(start, stop[, step]).

This fixes the problem with randint() which includes the
endpoint; in Python this is usually not what you want.
Do not supply the 'int', 'default', and 'maxwidth' arguments.
--

As you can see it takes quite a few more than one...

> To do this, this code should work:

times = 1
while times < 7:
 import random
 lottery_numbers=random.randrange(1, 42)
 print lottery_numbers
 times+=1


It would be better to have the import outside the loop.
Also for a fixed number of iterations a for loop is
more common:

import random
for n in range(7):
print random.randrange(1,42)

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


Re: [Tutor] 6 random numbers

2011-10-16 Thread Robert Sjoblom
>> hello all,
>> anyone know how i would go about printing 6 random numbers, i know i could 
>> copy and paste 6 times (which would work) but i was thinking about a while 
>> loop, ie. while lottery_numbers.count is <7.
>> Is it possible to code this? is it possible to count random variables? i am 
>> trying to keep the program as simple as possible, cheers
>>
>> any help would be welcome,
>>
>> import random
>> lottery_numbers=random.randrange(1,42)
>> print lottery_numbers
>>
>
> Following example may be useful:
> x = [random.randrange(1, 1+random.randrange(42) for _ in range(100)]
>
> Useful read:
> http://docs.python.org/tutorial/datastructures.html

This wouldn't work because if you're making a lottery-type program,
you can't generate the same number again. A solution would be to check
if, say we stored the numbers in a list, len(set(numbers)) == 7 and if
it's not append another (random) number. This would be done until
len(set(numbers)) == 7. However, it's a bit tedious and the random
module offers a much better option:

import random
numbers = random.sample(range(1,42), 7)

random.sample returns k unique random elements from a population
sequence; in this case the population sequence is range(1, 42) (I
think python 2.x it'd be xrange()?) and the second argument would be
the number of elements we want.

>>>random.sample(range(1,42), 7)
[16, 29, 17, 2, 12, 36, 10]#this list is in selection order!

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


Re: [Tutor] httplib2.RelativeURIError: Only absolute URIs are allowed. uri

2011-10-16 Thread Steven D'Aprano

pierre dagenais wrote:

Isn't "file:///home/pierre/bookmarks.html" a valid URI? It works fine 
with urllib, why not with httplib2?



"file://..." is not a HTTP resource.



--
Steven

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


Re: [Tutor] 6 random numbers

2011-10-16 Thread Steven D'Aprano

ADRIAN KELLY wrote:

hello all,
anyone know how i would go about printing 6 random numbers, i know i could copy and paste 6 times (which would work) but i was thinking about a while loop, ie. while lottery_numbers.count is <7. 
Is it possible to code this? is it possible to count random variables? i am trying to keep the program as simple as possible, cheers


any help would be welcome, 


import random
lottery_numbers=random.randrange(1,42)
print lottery_numbers


Whenever you want to do something repeatedly, a for-loop or while-loop 
is usually the answer. If you know how many times you need to do it, use 
a for-loop:


for i in range(6):
print random.randrange(1,42)


If you don't know how many times, use a while-loop:

total = 0
while total < 100:
total += random.randrange(1,42)
print total


If you want to collect the intermediate results, either store them in a 
list:


results = []
for i in range(6):
results.append(random.randrange(1,42))

or use a list comprehension, which is syntactic sugar for a for-loop:

results = [random.randrange(1,42) for i in range(6)]



--
Steven

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


Re: [Tutor] 6 random numbers

2011-10-16 Thread Stefan Behnel

ADRIAN KELLY, 16.10.2011 21:43:

anyone know how i would go about printing 6 random numbers


print(75, 45, 6, 35, 36472, 632)

Numbers were generated by typing randomly on my keyboard.

Sorry-for-taking-it-all-too-literally-ly,

Stefan

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