Re: [Tutor] if statement

2010-11-02 Thread Alan Gauld


"Glen Clark"  wrote 


On a side note.. I didn't know you could do something like this:

x = z if 


Yes it's python's equivalent to the C style 


foo = testVal() ? bar : baz

Also found in other languages (Java, Javascript, Perl?)

Alan G.

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


Re: [Tutor] if statement

2010-11-02 Thread Robert Berman


> -Original Message-
> From: tutor-bounces+bermanrl=cfl.rr@python.org [mailto:tutor-
> bounces+bermanrl=cfl.rr@python.org] On Behalf Of Glen Clark
> Sent: Tuesday, November 02, 2010 3:07 PM
> To: python mail list
> Subject: Re: [Tutor] if statement
> 
> sorry:
> 
> 
> confirmed = int(input("Are you happy with this? (y/n): ")
> 
> if confirmed == "y":
>for In in range(0,NumItems):
>   print(Entries[In] + ": " + str(In))
>change = int(input("Which item would you like to change: ")
>Entries[change]=str(input("Please enter a nem name: ")
> else:
> #do nothing

Whoops. Why are you trying to make the variable "confirmed" an integer rather
than a simple 'y/n' as you are asking for?

Robert Berman





--
I am using the free version of SPAMfighter.
We are a community of 7 million users fighting spam.
SPAMfighter has removed 34 of my spam emails to date.
Get the free SPAMfighter here: http://www.spamfighter.com/len

The Professional version does not have this message


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


Re: [Tutor] if statement

2010-11-02 Thread bob gailer

On 11/2/2010 3:07 PM, Glen Clark wrote:

sorry:

NumItems = int(input("How many Items: "))


Entries = []
for In in range(0,NumItems):
Entries.append("")



for In in range(0,NumItems):
Entries[In]=str(input("Enter name " + str(In+1) + ": "))


for In in range(0,NumItems):
print(Entries[In])

confirmed = int(input("Are you happy with this? (y/n): ")

In addition to the other help - do not use int here. You will get a run 
time error when it tries to convert y or n to an integer. If that 
succeeded e.g. the user entered 3), then confirmed (an integer) will 
NEVER be == "y" (a string).

if confirmed == "y":
for In in range(0,NumItems):
   print(Entries[In] + ": " + str(In))
change = int(input("Which item would you like to change: ")
Entries[change]=str(input("Please enter a nem name: ")
else:
 #do nothing

print(Entries)

On Tue, 2010-11-02 at 15:05 -0400, Alex Hall wrote:

On 11/2/10, Glen Clark  wrote:

  File "/home/glen/workspace/test.py", line 19
 if confirmed == "y":
^
SyntaxError: invalid syntax

Why does this not work??? PyDev says "Expected:else"

It may help to see the rest of the file, or at least more of the code
surrounding this statement.

___
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




--
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] if statement

2010-11-02 Thread Adam Bark

On 02/11/10 19:36, christopher.h...@allisontransmission.com wrote:



Glen Clark wrote on 11/02/2010 03:07:18 PM:

in general you should use raw_input instead of input.


> confirmed = int(input("Are you happy with this? (y/n): ")
>

It would appear that Glen is using python 3.x as he used the print 
function so input is correct.


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


Re: [Tutor] if statement

2010-11-02 Thread Alan Gauld


"Glen Clark"  wrote


confirmed = int(input("Are you happy with this? (y/n): ")

if confirmed == "y":


count the parens.

It thinks you are trying to do:

confimed = int(input('') if foo else bar)

HTH,


--
Alan Gauld
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] if statement

2010-11-02 Thread Emile van Sebille

On 11/2/2010 12:07 PM Glen Clark said...

sorry:

NumItems = int(input("How many Items: "))


Entries = []
for In in range(0,NumItems):
Entries.append("")



for In in range(0,NumItems):
Entries[In]=str(input("Enter name " + str(In+1) + ": "))


for In in range(0,NumItems):
print(Entries[In])

confirmed = int(input("Are you happy with this? (y/n): ")


The line above (which would appear to set confirmed to an int) doesn't 
have matching parens, which causes an error to be trapped elsewhere.


Emile

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


Re: [Tutor] if statement

2010-11-02 Thread Joel Goldstick
On Tue, Nov 2, 2010 at 3:34 PM, Adam Bark  wrote:

> On 02/11/10 19:07, Glen Clark wrote:
>
>> sorry:
>>
>> NumItems = int(input("How many Items: "))
>>
>>
>> Entries = []
>> for In in range(0,NumItems):
>>Entries.append("")
>>
>>
>>
>> for In in range(0,NumItems):
>>Entries[In]=str(input("Enter name " + str(In+1) + ": "))
>>
>>
>> for In in range(0,NumItems):
>>print(Entries[In])
>>
>> confirmed = int(input("Are you happy with this? (y/n): ")
>>
>> if confirmed == "y":
>>for In in range(0,NumItems):
>>   print(Entries[In] + ": " + str(In))
>>change = int(input("Which item would you like to change: ")
>>Entries[change]=str(input("Please enter a nem name: ")
>> else:
>> #do nothing
>>
>> print(Entries)
>>
>>
>>
> Ok that's easier, you're missing a closing bracket at the end of the
>
>
> confirmed = int(input("Are you happy with this? (y/n): ")
>
> line.
> I think you might need at least a pass after else as well, although it
> should be fine to leave it out altogether once you've added the ')'.
>
> HTH,
> Adam.
>
> ___
>
if confirmed == "y":
   for In in range(0,NumItems):
  print(Entries[In] + ": " + str(In))
   change = int(input("Which item would you like to change: ")
   Entries[change]=str(input("Please enter a nem name: ")

> else:
> #do nothing
>
Actually, I caught two mismatched parens.  In the line starting with
'change' and the following line.

Fix those first


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



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


Re: [Tutor] if statement

2010-11-02 Thread christopher . henk
Glen Clark wrote on 11/02/2010 03:07:18 PM:

in general you should use raw_input instead of input.


> confirmed = int(input("Are you happy with this? (y/n): ")
> 

you are missing a closing parenthesis above, and converting a string (y,n) 
to and int
> if confirmed == "y":

you are comparing an int and a string here, probably not what you want 
(see above).

>for In in range(0,NumItems):
>   print(Entries[In] + ": " + str(In))
>change = int(input("Which item would you like to change: ") 

again, you are missing a closing parenthesis above

>Entries[change]=str(input("Please enter a nem name: ")

again, you are missing a closing parenthesis above

> else:
> #do nothing

you need to put pass here, you can't just have a comment in a block
> 
> print(Entries)
> 


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


Re: [Tutor] if statement

2010-11-02 Thread Glen Clark
Okay, 

Thank you very much for the help guys :)

On a side note.. I didn't know you could do something like this:

x = z if 

(thus why I needed the closing parenthesis)

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


Re: [Tutor] if statement

2010-11-02 Thread Joel Goldstick
On Tue, Nov 2, 2010 at 3:17 PM, Glen Clark  wrote:

> I tried that and it still does not work.
>
> On Tue, 2010-11-02 at 15:13 -0400, James Reynolds wrote:
> > the syntax is:
> >
> >
> > if True:
> >  do something that you want to do if the condition you are testing
> > is True, in your case when confirmed is "y"
> > elif True:
> >  optional: do something else when the above condition is false and
> > this condition is True.
> > else:
> >  do something else when the above conditions are false.
> >
> >
> > You can try this,
> >
> >
> > if confirmed == "y":
> > stuff
> > else:
> > pass
> >
> >
> >
> >
> > On Tue, Nov 2, 2010 at 3:02 PM, Glen Clark  wrote:
> >  File "/home/glen/workspace/test.py", line 19
> >if confirmed == "y":
> >   ^
> > SyntaxError: invalid syntax
> >
> > Why does this not work??? PyDev says "Expected:else"
> >
> > ___
> > Tutor maillist  -  Tutor@python.org
> > To unsubscribe or change subscription options:
> > http://mail.python.org/mailman/listinfo/tutor
> >
> >
>
>   change = int(input("Which item would you like to change: ")

In the line about you have mismatched parentheses.  This confuses the parser

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



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


Re: [Tutor] if statement

2010-11-02 Thread Vince Spicer
On Tue, Nov 2, 2010 at 1:07 PM, Glen Clark  wrote:

> sorry:
>
> NumItems = int(input("How many Items: "))
>
>
> Entries = []
> for In in range(0,NumItems):
>   Entries.append("")
>
>
>
> for In in range(0,NumItems):
>   Entries[In]=str(input("Enter name " + str(In+1) + ": "))
>
>
> for In in range(0,NumItems):
>   print(Entries[In])
>
> confirmed = int(input("Are you happy with this? (y/n): ")
>
> if confirmed == "y":
>   for In in range(0,NumItems):
>  print(Entries[In] + ": " + str(In))
>   change = int(input("Which item would you like to change: ")
>   Entries[change]=str(input("Please enter a nem name: ")
> else:
>#do nothing
>
> print(Entries)
>
> On Tue, 2010-11-02 at 15:05 -0400, Alex Hall wrote:
> > On 11/2/10, Glen Clark  wrote:
> > >  File "/home/glen/workspace/test.py", line 19
> > > if confirmed == "y":
> > >^
> > > SyntaxError: invalid syntax
> > >
> > > Why does this not work??? PyDev says "Expected:else"
> > It may help to see the rest of the file, or at least more of the code
> > surrounding this statement.
> > >
> > > ___
> > > 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
>

confirmed = int(input("Are you happy with this? (y/n): ")

You are missing a ) that the end of this line to close the int

-- 
Vince Spicer


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


Re: [Tutor] if statement

2010-11-02 Thread Adam Bark

On 02/11/10 19:07, Glen Clark wrote:

sorry:

NumItems = int(input("How many Items: "))


Entries = []
for In in range(0,NumItems):
Entries.append("")



for In in range(0,NumItems):
Entries[In]=str(input("Enter name " + str(In+1) + ": "))


for In in range(0,NumItems):
print(Entries[In])

confirmed = int(input("Are you happy with this? (y/n): ")

if confirmed == "y":
for In in range(0,NumItems):
   print(Entries[In] + ": " + str(In))
change = int(input("Which item would you like to change: ")
Entries[change]=str(input("Please enter a nem name: ")
else:
 #do nothing

print(Entries)

   

Ok that's easier, you're missing a closing bracket at the end of the

confirmed = int(input("Are you happy with this? (y/n): ")

line.
I think you might need at least a pass after else as well, although it 
should be fine to leave it out altogether once you've added the ')'.


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


Re: [Tutor] if statement

2010-11-02 Thread Glen Clark
I tried that and it still does not work. 

On Tue, 2010-11-02 at 15:13 -0400, James Reynolds wrote:
> the syntax is:
> 
> 
> if True:
>  do something that you want to do if the condition you are testing
> is True, in your case when confirmed is "y"
> elif True:
>  optional: do something else when the above condition is false and
> this condition is True.
> else:
>  do something else when the above conditions are false.
> 
> 
> You can try this,
> 
> 
> if confirmed == "y":
> stuff
> else:
> pass
> 
> 
> 
> 
> On Tue, Nov 2, 2010 at 3:02 PM, Glen Clark  wrote:
>  File "/home/glen/workspace/test.py", line 19
>if confirmed == "y":
>   ^
> SyntaxError: invalid syntax
> 
> Why does this not work??? PyDev says "Expected:else"
> 
> ___
> 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] if statement

2010-11-02 Thread James Reynolds
the syntax is:

if True:
 do something that you want to do if the condition you are testing is
True, in your case when confirmed is "y"
elif True:
 optional: do something else when the above condition is false and this
condition is True.
else:
 do something else when the above conditions are false.

You can try this,

if confirmed == "y":
stuff
else:
pass



On Tue, Nov 2, 2010 at 3:02 PM, Glen Clark  wrote:

>  File "/home/glen/workspace/test.py", line 19
>if confirmed == "y":
>   ^
> SyntaxError: invalid syntax
>
> Why does this not work??? PyDev says "Expected:else"
>
> ___
> 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] if statement

2010-11-02 Thread delegbede
It is not always enough to send just the error. 
What construct do you have after the if line?

Make your sample detailed to get useful responses. 

Regards. 
Sent from my BlackBerry wireless device from MTN

-Original Message-
From: Glen Clark 
Sender: tutor-bounces+delegbede=dudupay@python.org
Date: Tue, 02 Nov 2010 19:02:15 
To: python mail list
Subject: [Tutor] if statement

 File "/home/glen/workspace/test.py", line 19
if confirmed == "y":
   ^
SyntaxError: invalid syntax

Why does this not work??? PyDev says "Expected:else"

___
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] if statement

2010-11-02 Thread Adam Bark

On 02/11/10 19:02, Glen Clark wrote:

  File "/home/glen/workspace/test.py", line 19
 if confirmed == "y":
^
SyntaxError: invalid syntax

Why does this not work??? PyDev says "Expected:else"
   

It's hard to tell without context. Can you send the code around it as well?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] if statement

2010-11-02 Thread Glen Clark
sorry:

NumItems = int(input("How many Items: "))


Entries = []
for In in range(0,NumItems):
   Entries.append("")



for In in range(0,NumItems):
   Entries[In]=str(input("Enter name " + str(In+1) + ": "))
   

for In in range(0,NumItems):
   print(Entries[In])

confirmed = int(input("Are you happy with this? (y/n): ")

if confirmed == "y":
   for In in range(0,NumItems):
  print(Entries[In] + ": " + str(In))
   change = int(input("Which item would you like to change: ")   
   Entries[change]=str(input("Please enter a nem name: ")
else:
#do nothing

print(Entries)

On Tue, 2010-11-02 at 15:05 -0400, Alex Hall wrote:
> On 11/2/10, Glen Clark  wrote:
> >  File "/home/glen/workspace/test.py", line 19
> > if confirmed == "y":
> >^
> > SyntaxError: invalid syntax
> >
> > Why does this not work??? PyDev says "Expected:else"
> It may help to see the rest of the file, or at least more of the code
> surrounding this statement.
> >
> > ___
> > 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] if statement

2010-11-02 Thread Alex Hall
On 11/2/10, Glen Clark  wrote:
>  File "/home/glen/workspace/test.py", line 19
> if confirmed == "y":
>^
> SyntaxError: invalid syntax
>
> Why does this not work??? PyDev says "Expected:else"
It may help to see the rest of the file, or at least more of the code
surrounding this statement.
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>


-- 
Have a great day,
Alex (msg sent from GMail website)
mehg...@gmail.com; http://www.facebook.com/mehgcap
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] if statement

2010-11-02 Thread Glen Clark
 File "/home/glen/workspace/test.py", line 19
if confirmed == "y":
   ^
SyntaxError: invalid syntax

Why does this not work??? PyDev says "Expected:else"

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


Re: [Tutor] mutable types

2010-11-02 Thread Steven D'Aprano

John wrote:

Hello, I thought the following should end with G[1] and G[0] returning
20. Why doesn't it?

In [69]: a = 10
In [70]: G = {}
In [71]: G[0] = [a]
In [72]: G[1] = G[0]
In [73]: a = 20
In [74]: G[1]
Out[74]: [10]
In [75]: G[0]
Out[75]: [10]


This doesn't actually have anything to do with the difference between 
mutable and immutable objects. Here's a simpler example to think about:


a = 10  # the name "a" is bound to the object 10
b = a  # the name "b" is bound to the same object "a" is bound to
a = 20  # the name "a" is bound to the object 20
b = 

What value would you expect b to have?

The fact that you are using a mutable dictionary in your example is no 
different:


a = 10  # the name "a" is bound to the object 10
G = {}  # the name "G" is bound to an empty dictionary object
G[0] = [a]  # the dictionary bound to G has a mapping added,
  # the key 0 is mapped to a list containing the object bound to "a"

The important thing here is that the list [a] doesn't, and can't, know 
anything about the *name* "a". All it sees is the object that a is 
*currently* bound to. Once that object is placed in the list, there is 
no connection from the name "a" to the list, or visa versa. So later on, 
when you re-bind "a" to 20, that does nothing to the object 10, or the 
list that contains 10, or the dict that contains the list.


Around this time, many people start arguing about "call by value" and 
"call by reference" (or sometimes "pass by value" and "pass by 
reference"). If these terms mean nothing to you, congratulations! That's 
one less thing for you to care about, and you can stop reading now and 
go and do something productive. Arguments about calling conventions in 
programming languages often sink into long, unproductive flame wars.


Seriously. Stop reading and go do some programming.

Still here? Okay, don't say you weren't warned...

If you do know the terms "call by (pass by) reference" and "call by 
value", and you're thinking that Python is pass-by-reference for mutable 
objects, no, it's not. There is no way to get this behaviour in Python, 
regardless of whether you use mutable or immutable objects:


# this doesn't work
a = 1
b = 2
swap(a, b)  # call a function with by-reference parameters
assert a == 2  # fails
assert b == 1  # also fails

Can't be done from Python. There are alternatives that work just as 
well, such as the idiomatic:


a, b = b, a

but Python has no by-reference variables or arguments.

Now you're thinking that Python must be pass-by-value. If you're not, 
congratulations, you've just dodged a very common mistake. Python isn't 
pass-by-value *either*:


def func(alist):
del alist[:]
return alist

# this doesn't work either
a = [1,2,3]
b = func(a)  # pass-by-value automatically makes a copy of a
assert a == [1,2,3] and b = []  # fails

Python's evaluation strategy goes by various names, my favourite being 
pass-by-object. Another is call-by-sharing. Unfortunately, other 
languages fall into the trap of thinking that there are only two 
strategies, call-by-value and call-by-reference, and so they use one or 
the other of those names for the exact same behaviour that Python 
exhibits. Sigh.


http://en.wikipedia.org/wiki/Evaluation_strategy




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


Re: [Tutor] mutable types

2010-11-02 Thread Knacktus

Am 02.11.2010 13:51, schrieb John:

Hello, I thought the following should end with G[1] and G[0] returning
20. Why doesn't it?

In [69]: a = 10
"a" is a reference to an immutable object of type int with value 10. 
Somewhere in memory is an object of type int with value 10 stored. This 
object cannot be changed. "a" is just a name that references to the 
memory adress.

In [70]: G = {}
In [71]: G[0] = [a]
now, G[0] holds a reference to a mutable object of type list with an 
immutable object of type int with value 10 as its content. So, your list 
holds as item the memory adress of an object int with value 10. Your 
list knows nothing about other names (like "a"), that refer to the same 
adress in memory. You have handed over the object to the list (or memory 
adress of the object), not the name "a".

In [72]: G[1] = G[0]
In [73]: a = 20

Now, you've created a *new* immutable object of type int with value 20.
"a" now refers to the adress of the new integer object. "a" has 
forgotten everything about its former reference to the object of type 
int with value 10.
You have *not* changed the "old" object of type int with value 10. Your 
list in G[0] still holds the memory adress of the first int with value 10.

In [74]: G[1]
Out[74]: [10]
In [75]: G[0]
Out[75]: [10]

??
Now, if you would keep a reference to the list you've handed over to the 
dict, you could change the content of the list, as the list is mutable.


>>> a = [10]
>>> G = {}
>>> G[0] = a
>>> G[0]
[10]
>>> a[0] = 20
>>> G[0]
[20]

Take care of some operations that return a copy of the list:

>>> a = a[:]
Now, "a" refernces to a new list, which has also one item of type in 
with value 20. But it's not the same list as originally!

>>> a[0] = 30
Changing this new list ...
>>> G[0]
[20]
does not affect the list, we stored in the dict.

HTH,

Jan

___
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] mutable types

2010-11-02 Thread John
Hello, I thought the following should end with G[1] and G[0] returning
20. Why doesn't it?

In [69]: a = 10
In [70]: G = {}
In [71]: G[0] = [a]
In [72]: G[1] = G[0]
In [73]: a = 20
In [74]: G[1]
Out[74]: [10]
In [75]: G[0]
Out[75]: [10]

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


Re: [Tutor] True Random Numbers

2010-11-02 Thread Steven D'Aprano

Crallion wrote:
In an attempt to generate "true" random numbers, 


Have you considered using your operating system's source of random numbers?

random.SystemRandom and os.urandom return numbers which are 
cryptographically sound. They are also based on various sources of 
physical randomness (which is sometimes a disadvantage, as the entropy 
can run out and then the functions will hang waiting for more entropy to 
be collected).



> I found this python script- http://code.google.com/p/truerandom/

Getting to the point, I get this error when I run my program.

File "/dev/python/absolute/truerandom.py", line 9, in 
from urllib import urlopen
ImportError: cannot import name urlopen

Is urllib and/or urlopen deprecated in 3.1.2 or is it something I forgot to do?
Python is running on OSX 10.6, which I upgraded from the default install to 
3.1.2.


You should never upgrade the system Python by hand, because parts of the 
OS are likely to expect a specific version. You should install it 
separately.


But in this case, something is screwy, because that script is written 
for Python 2.x and won't run at all under Python 3.x. The fact that you 
managed to get as far as an ImportError is weird -- it should fail with 
SyntaxError during compilation.


urllib still exists in Python 3.x, but it has been re-organised and 
urlopen is now called something else. Instead of


from urllib import urlopen

you need

from urllib.request import urlopen

See the Fine Manual for further details.


--
Steven

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


Re: [Tutor] True Random Numbers

2010-11-02 Thread Christian Witts

On 02/11/2010 11:29, Crallion wrote:

In an attempt to generate "true" random numbers, I found this python script- 
http://code.google.com/p/truerandom/
Getting to the point, I get this error when I run my program.

File "/dev/python/absolute/truerandom.py", line 9, in
 from urllib import urlopen
ImportError: cannot import name urlopen

Is urllib and/or urlopen deprecated in 3.1.2 or is it something I forgot to do?
Python is running on OSX 10.6, which I upgraded from the default install to 
3.1.2.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

   


|The urllib2 module has been split across several modules in Python 3.0 
named urllib.request and urllib.error so you should be doing `from 
urllib.request import urlopen`.|


--
Kind Regards,
Christian Witts


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


Re: [Tutor] Complete Shutdown

2010-11-02 Thread Timo

On 01-11-10 21:01, Chris King wrote:

 Dear Tutors,
How do I completely shutdown a computer without administrative 
rights using a simple python script.

I once came across the following when playing with dbus.

import dbus
bus = dbus.SystemBus()
bus_object = bus.get_object("org.freedesktop.Hal", 
"/org/freedesktop/Hal/devices/computer")

bus_object.Shutdown(dbus_interface="org.freedesktop.Hal.Device.SystemPowerManagement")

This will shutdown your computer immediatly. You can substitute the 
"Shutdown" method with "Suspend", "Hibernate" or "Reboot".


Cheers,
Timo



Sincerely,
Me, Myself, and I
___
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] True Random Numbers

2010-11-02 Thread Crallion
In an attempt to generate "true" random numbers, I found this python script- 
http://code.google.com/p/truerandom/
Getting to the point, I get this error when I run my program.

File "/dev/python/absolute/truerandom.py", line 9, in 
from urllib import urlopen
ImportError: cannot import name urlopen

Is urllib and/or urlopen deprecated in 3.1.2 or is it something I forgot to do?
Python is running on OSX 10.6, which I upgraded from the default install to 
3.1.2.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] unittest + tkinter

2010-11-02 Thread Alan Gauld


"Wayne Werner"  wrote

I'm trying my hand at test driven development via the unittest 
module. Since
my program is using Tkinter, my thought is that I need to create the 
main
window. The only problem is that when I launch the main loop, 
further lines

of code aren't executed.


Thats a feature of event driven programs. All the action takes place
inside the event loop. I don't know how you drive a GUI with unittest
though so can't help solve your problem. But the behaviour is normal
for any GUI.

Alan G.


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