[Tutor] Getting confusing NameError

2010-08-18 Thread shane brennan
Hi Tutors

This is my first mail to this list so firstly thank you for reading and
apologies in advance for any noob mistakes i may have inadvertantly made:).
Ive only started learning python as my first programming language and its
all going well so far, working my way through a couple of books one of which
is programming python: for absolute beginners by Michael Dawson

In one of the code samples he supplies while doing loops ( namely if and
else loops) im getting a NameError i dont understand and cannot find any
help anywhere about it. here is the code he supplied and the error im
getting

# Password
# Demonstrates the if statement

print("Welcome to System Security Inc.")
print("-- where security is our middle name\n")

password = input("Enter your password: ")

if password == "secret":
print("Access Granted")

input("\n\nPress the enter key to exit.")


and the traceback error im getting

Traceback (most recent call last):
  File "G:\Programming\Python\programming python\chapter3\password.py", line
7,
in 
password = input("Enter your password: ")
  File "", line 1, in 
NameError: name 'secret' is not defined


I know this is probably very simple but for some reason i cannot get my head
around it.

thank you for any help in advance

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


Re: [Tutor] Getting confusing NameError

2010-08-18 Thread Evert Rol
> This is my first mail to this list so firstly thank you for reading and 
> apologies in advance for any noob mistakes i may have inadvertantly made:). 
> Ive only started learning python as my first programming language and its all 
> going well so far, working my way through a couple of books one of which is 
> programming python: for absolute beginners by Michael Dawson
>  
> In one of the code samples he supplies while doing loops ( namely if and else 
> loops) im getting a NameError i dont understand and cannot find any help 
> anywhere about it. here is the code he supplied and the error im getting
>  
> # Password
> # Demonstrates the if statement
>  
> print("Welcome to System Security Inc.")
> print("-- where security is our middle name\n")
>  
> password = input("Enter your password: ")
>  
> if password == "secret":
> print("Access Granted")
>  
> input("\n\nPress the enter key to exit.")
>  
>  
> and the traceback error im getting
>  
> Traceback (most recent call last):
>   File "G:\Programming\Python\programming python\chapter3\password.py", line 
> 7,
> in 
> password = input("Enter your password: ")
>   File "", line 1, in 
> NameError: name 'secret' is not defined


  Hi Shane,

Normally, a NameError would indicate a variable that's not defined. Here it's a 
it more tricky. You're probably using Python 2.x, while the code is for Python 
3.x. There are several changes between Python versions 2 and 3 that make the 
two languages different (or rather, the interpreters handle some code 
differently). One difference how the function 'input()' is handled, and that 
difference is big enough for your code to crash. 
I would hope that the book somewhere in the beginning mentions the code is for 
Python 3 and not for Python 2. The best solution for you is to try and install 
Python 3 and use that to proceed through the code in the book.

  Evert

NB: if-else are not actually loops, but rather form a conditional statement. 
You can, then, use such a if-else construction to break out of a loop, which 
probably will be shown in some later code. Just to be pedantic ;-).


> I know this is probably very simple but for some reason i cannot get my head 
> around it.
>  
> thank you for any help in advance
>  
> shane
> ___
> 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] Getting confusing NameError

2010-08-18 Thread Nitin Das
use raw_input instead of input . as using input is unsafe as it evaluates
your input as python command.
for e,g.
x = input("enter command") and u entered "1 + 2" as input., then it will
evaluate 1+2 = 3 = x.

 -nitin

On Wed, Aug 18, 2010 at 12:45 PM, shane brennan wrote:

> Hi Tutors
>
> This is my first mail to this list so firstly thank you for reading and
> apologies in advance for any noob mistakes i may have inadvertantly made:).
> Ive only started learning python as my first programming language and its
> all going well so far, working my way through a couple of books one of which
> is programming python: for absolute beginners by Michael Dawson
>
> In one of the code samples he supplies while doing loops ( namely if and
> else loops) im getting a NameError i dont understand and cannot find any
> help anywhere about it. here is the code he supplied and the error im
> getting
>
> # Password
> # Demonstrates the if statement
>
> print("Welcome to System Security Inc.")
> print("-- where security is our middle name\n")
>
> password = input("Enter your password: ")
>
> if password == "secret":
> print("Access Granted")
>
> input("\n\nPress the enter key to exit.")
>
>
> and the traceback error im getting
>
> Traceback (most recent call last):
>   File "G:\Programming\Python\programming python\chapter3\password.py",
> line 7,
> in 
> password = input("Enter your password: ")
>   File "", line 1, in 
> NameError: name 'secret' is not defined
>
>
> I know this is probably very simple but for some reason i cannot get my
> head around it.
>
> thank you for any help in advance
>
> shane
>
> ___
> 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] Getting confusing NameError

2010-08-18 Thread Christian Witts

On 18/08/2010 09:15, shane brennan wrote:

Hi Tutors
This is my first mail to this list so firstly thank you for reading 
and apologies in advance for any noob mistakes i may have 
inadvertantly made:). Ive only started learning python as my first 
programming language and its all going well so far, working my way 
through a couple of books one of which is programming python: for 
absolute beginners by Michael Dawson
In one of the code samples he supplies while doing loops ( namely if 
and else loops) im getting a NameError i dont understand and cannot 
find any help anywhere about it. here is the code he supplied and the 
error im getting

# Password
# Demonstrates the if statement
print("Welcome to System Security Inc.")
print("-- where security is our middle name\n")
password = input("Enter your password: ")
if password == "secret":
print("Access Granted")
input("\n\nPress the enter key to exit.")
and the traceback error im getting
Traceback (most recent call last):
  File "G:\Programming\Python\programming 
python\chapter3\password.py", line 7,

in 
password = input("Enter your password: ")
  File "", line 1, in 
NameError: name 'secret' is not defined
I know this is probably very simple but for some reason i cannot get 
my head around it.

thank you for any help in advance
shane


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


input() [1] expects a valid Python expression as input as it gets 
evaluated and is not technically safe.

raw_input() [2] is what you should be using instead.

[1] http://docs.python.org/library/functions.html#input
[2] http://docs.python.org/library/functions.html#raw_input

Hope that helps.  Welcome to the list and enjoy your stay.

--
Kind Regards,
Christian Witts


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


Re: [Tutor] box drawing characters

2010-08-18 Thread Alan Gauld


"Bill Allen"  wrote
I want to be able to create some user interfaces, similar to what 
you see
in console based programs like Midnight Commander or other TUI type 
programs
that use box drawing characters to create a user interfaces for 
input and

display of information.


This is OK and you can use curses on Linux/Unix systems and something
like Conio on Windows, but


However, I do want them to be portable to most
platforms that Python will run on.


This is much more difficult. Terminals are non portable and neither
of the libraries above work on the other system. You would probably
have to build your UI twice. This is one area where real GUIs work
better than a terminal.

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] Error Using exec to Run Module Files

2010-08-18 Thread Alan Gauld


 wrote


exec(open('script1.py').read())

Traceback (most recent call last):
 File "", line 1, in 
 File "", line 1
   %!PS-Adobe-3.0
   ^
SyntaxError: invalid syntax

What is going on/


Looks like your script1.py file is actually a postscript file.
exec expects to see valid Python code, it can't process Postscript.

Can you run script1.py from the command line?

Or can you import it at the >>> prompt?

HTH,

Alan G.


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


Re: [Tutor] Getting confusing NameError

2010-08-18 Thread Alan Gauld

"shane brennan"  wrote

In one of the code samples he supplies while doing loops ( namely if 
and
else loops) im getting a NameError i dont understand and cannot find 
any

help anywhere about it.


Evert has already pointed out that the code is for Python v3 and it
looks like you are using Python v2.

If your other book is based on v2 then using the two side by side will
be confusing. If both are on v3 then you need to upgrade your Python
to v3. If they are mixed versions then you need to decide on only one
book and follow whichever version it uses.

You can find both v2 and v3 versions of my tutorial on my web site.

--
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] Getting confusing NameError

2010-08-18 Thread shane brennan
Hi all

Thank you for all the help. I had upgraded the machine to v3 but for some
reason 2 hadnt removed properly, once i did some digigng i found the
redundant entries in the registry and the libraries that didnt remove and
cleared them up . script is running fine now:) thanks for all the help

Espcially thanks for the pointers and information on the difference
between raw_input and input.

again thanks for everything, and hopefully ill be around here for a long
time and once i get a more experienced i can contribute back

On Wed, Aug 18, 2010 at 09:37, Alan Gauld  wrote:

> "shane brennan"  wrote
>
>  In one of the code samples he supplies while doing loops ( namely if and
>> else loops) im getting a NameError i dont understand and cannot find any
>> help anywhere about it.
>>
>
> Evert has already pointed out that the code is for Python v3 and it
> looks like you are using Python v2.
>
> If your other book is based on v2 then using the two side by side will
> be confusing. If both are on v3 then you need to upgrade your Python
> to v3. If they are mixed versions then you need to decide on only one
> book and follow whichever version it uses.
>
> You can find both v2 and v3 versions of my tutorial on my web site.
>
> --
> 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
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Need help understanding output...

2010-08-18 Thread Laurens Vets

On 8/12/2010 1:26 AM, Steven D'Aprano wrote:

On Thu, 12 Aug 2010 07:04:15 am Laurens Vets wrote:


I need to generate a list of 30 numbers randomly chosen from 1, 2, 3,
4, 5&  6. However, I cannot have more than 2 numbers which are the
same next to each other. I came up with the following (Please ignore
the fact that I'm trying to avoid an IndexError in a stupid way :)):


I can't possible do that! :)


import random
reeks = []
while len(reeks)<= 1:
number = random.randrange(1, 7, 1)
reeks.append(number)

while len(reeks)<= 29:
nummer = random.randrange(1, 7, 1)
if nummer != reeks[-1] and nummer != reeks[-2]:
  reeks.append(nummer)
print reeks


This is probably a simpler way:

import random
reeks = []
for i in range(30):
 temp = [random.randint(1, 6)]
 while reeks[-2:-1] == reeks[-1:] == temp:
 # print "Triplet found:", reeks, temp
 temp = [random.randint(1, 6)]
 reeks.extend(temp)

print reeks




Thank you all for your help! I've added another condition to this 
program, namely, in a range of 60, each 'random' number can only occur 
10 times. I came up with the following:


import random
series = []
for i in range(60):
  temp = [random.randint(1, 6)]
  while series[-2:-1] == series[-1:] == temp:
temp = [random.randint(1, 6)]
  while series.count(temp[0]) >= 10:
temp = [random.randint(1, 6)]
  series.extend(temp)
print series

However, this also generates gems such as:

[4, 4, 3, 6, 3, 2, 6, 3, 4, 3, 4, 1, 4, 2, 4, 3, 4, 4, 6, 2, 1, 5, 5, 6, 
5, 6, 5, 6, 3, 3, 1, 6, 6, 4, 1, 5, 2, 6, 6, 4, 2, 5, 3, 1, 5, 5, 2, 1, 
_2_, _2_, _2_, 3, 3, 2, 1, 5, 1, 1, 5, 1]


[1, 4, 3, 1, 5, 3, 5, 1, 5, 6, 5, 3, 2, 6, 1, 4, 1, 5, 5, 2, 6, 2, 4, 1, 
3, 1, 2, 1, 1, 3, 1, 6, 6, 3, 2, 3, 6, 4, 5, 6, 5, 6, 3, 6, 2, 4, 5, 3, 
3, 4, 6, _4_, _4_, _4_, 5, 4, _2_, _2_, _2_, _2_]


I thought this needed to become the following:

import random
series = []
for i in range(60):
  temp = [random.randint(1, 6)]
  print "Temp", i, ":", temp
  while series[-2:-1] == series[-1:] == series:
if series.count(temp[0]) >= 10:
  temp = [random.randint(1, 6)]
  series.extend(temp)
print series

But this just hangs whenever the while clause matches. I'm not sure what 
I'm doing wrong here. I do know the random.shuffle() function, but I 
can't put my conditions in there.


Any push in the right direction would be greatly appreciated :)
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Need help understanding output...

2010-08-18 Thread Evert Rol

On 18 Aug 2010, at 13:21 , Laurens Vets wrote:

> On 8/12/2010 1:26 AM, Steven D'Aprano wrote:
>> On Thu, 12 Aug 2010 07:04:15 am Laurens Vets wrote:
>> 
>>> I need to generate a list of 30 numbers randomly chosen from 1, 2, 3,
>>> 4, 5&  6. However, I cannot have more than 2 numbers which are the
>>> same next to each other. I came up with the following (Please ignore
>>> the fact that I'm trying to avoid an IndexError in a stupid way :)):
>> 
>> I can't possible do that! :)
>> 
>>> import random
>>> reeks = []
>>> while len(reeks)<= 1:
>>>number = random.randrange(1, 7, 1)
>>>reeks.append(number)
>>> 
>>> while len(reeks)<= 29:
>>>nummer = random.randrange(1, 7, 1)
>>>if nummer != reeks[-1] and nummer != reeks[-2]:
>>>  reeks.append(nummer)
>>> print reeks
>> 
>> This is probably a simpler way:
>> 
>> import random
>> reeks = []
>> for i in range(30):
>> temp = [random.randint(1, 6)]
>> while reeks[-2:-1] == reeks[-1:] == temp:
>> # print "Triplet found:", reeks, temp
>> temp = [random.randint(1, 6)]
>> reeks.extend(temp)
>> 
>> print reeks
> 
> 
> 
> Thank you all for your help! I've added another condition to this program, 
> namely, in a range of 60, each 'random' number can only occur 10 times. I 
> came up with the following:
> 
> import random
> series = []
> for i in range(60):
>  temp = [random.randint(1, 6)]
>  while series[-2:-1] == series[-1:] == temp:
>temp = [random.randint(1, 6)]
>  while series.count(temp[0]) >= 10:
>temp = [random.randint(1, 6)]
>  series.extend(temp)
> print series
> 
> However, this also generates gems such as:
> 
> [4, 4, 3, 6, 3, 2, 6, 3, 4, 3, 4, 1, 4, 2, 4, 3, 4, 4, 6, 2, 1, 5, 5, 6, 5, 
> 6, 5, 6, 3, 3, 1, 6, 6, 4, 1, 5, 2, 6, 6, 4, 2, 5, 3, 1, 5, 5, 2, 1, _2_, 
> _2_, _2_, 3, 3, 2, 1, 5, 1, 1, 5, 1]
> 
> [1, 4, 3, 1, 5, 3, 5, 1, 5, 6, 5, 3, 2, 6, 1, 4, 1, 5, 5, 2, 6, 2, 4, 1, 3, 
> 1, 2, 1, 1, 3, 1, 6, 6, 3, 2, 3, 6, 4, 5, 6, 5, 6, 3, 6, 2, 4, 5, 3, 3, 4, 6, 
> _4_, _4_, _4_, 5, 4, _2_, _2_, _2_, _2_]
> 
> I thought this needed to become the following:
> 
> import random
> series = []
> for i in range(60):
>  temp = [random.randint(1, 6)]
>  print "Temp", i, ":", temp
>  while series[-2:-1] == series[-1:] == series:
>if series.count(temp[0]) >= 10:
>  temp = [random.randint(1, 6)]
>  series.extend(temp)
> print series
> 
> But this just hangs whenever the while clause matches. I'm not sure what I'm 
> doing wrong here. I do know the random.shuffle() function, but I can't put my 
> conditions in there.


I assume the '== series:' is a typo, and should be '== temp'. Otherwise I don't 
see how this matches, unless series = [] (which means the while clause always 
matches, right at the start. Perhaps that actually is what you're seeing?). 
That's probably the disadvantage of using a list to avoid an IndexError (sorry 
Steven; I would have just caught the IndexError, or have an 'if i < 2: 
continue' statement in there.)

As suggestion for avoiding occurrences of > 10 times: use a dict, where the 
random numbers become the keys and you add +1 every time to d[temp]. Checking 
if d[temp] > 10 is then very easy.


Cheers,

  Evert



> 
> Any push in the right direction would be greatly appreciated :)
> ___
> 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] Getting confusing NameError

2010-08-18 Thread Wayne Werner
On Wed, Aug 18, 2010 at 3:48 AM, shane brennan wrote:

> 
>
> again thanks for everything, and hopefully ill be around here for a long
> time and once i get a more experienced i can contribute back
>

You don't need to be /too/ experienced. Contributing here is actually how I
learned Python as much and as well as I did. When someone asked a question
(especially interesting ones), I made it a point to try and answer them. If
I didn't already know the answer I would go Google and try to find the
answer. Sometimes it was over my head and I wasn't able to form a decent
reply before others, but many times I was able to understand the problem and
question and fire off some fairly good information.

And if you preface your post with "I'm new to this part of Python, but I
looked it up and this is what I found..." or some other self effacing
statement that shows you're pretty confident in your answer but you're
perfectly willing to be taught if you were wrong - which will (usually) keep
you from looking like a cocky noob, and look like more like a humble acolyte
trying to do your best to help out a fellow Pythonista.

At least that's my experience ;)
-Wayne
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Need help understanding output...

2010-08-18 Thread Laurens Vets






Thank you all for your help! I've added another condition to this program, 
namely, in a range of 60, each 'random' number can only occur 10 times. I came 
up with the following:

import random
series = []
for i in range(60):
  temp = [random.randint(1, 6)]
  while series[-2:-1] == series[-1:] == temp:
temp = [random.randint(1, 6)]
  while series.count(temp[0])>= 10:
temp = [random.randint(1, 6)]
  series.extend(temp)
print series

However, this also generates gems such as:

[4, 4, 3, 6, 3, 2, 6, 3, 4, 3, 4, 1, 4, 2, 4, 3, 4, 4, 6, 2, 1, 5, 5, 6, 5, 6, 
5, 6, 3, 3, 1, 6, 6, 4, 1, 5, 2, 6, 6, 4, 2, 5, 3, 1, 5, 5, 2, 1, _2_, _2_, 
_2_, 3, 3, 2, 1, 5, 1, 1, 5, 1]

[1, 4, 3, 1, 5, 3, 5, 1, 5, 6, 5, 3, 2, 6, 1, 4, 1, 5, 5, 2, 6, 2, 4, 1, 3, 1, 
2, 1, 1, 3, 1, 6, 6, 3, 2, 3, 6, 4, 5, 6, 5, 6, 3, 6, 2, 4, 5, 3, 3, 4, 6, _4_, 
_4_, _4_, 5, 4, _2_, _2_, _2_, _2_]

I thought this needed to become the following:

import random
series = []
for i in range(60):
  temp = [random.randint(1, 6)]
  print "Temp", i, ":", temp
  while series[-2:-1] == series[-1:] == series:
if series.count(temp[0])>= 10:
  temp = [random.randint(1, 6)]
  series.extend(temp)
print series

But this just hangs whenever the while clause matches. I'm not sure what I'm 
doing wrong here. I do know the random.shuffle() function, but I can't put my 
conditions in there.


I assume the '== series:' is a typo, and should be '== temp'. Otherwise I don't 
see how this matches, unless series = [] (which means the while clause always 
matches, right at the start. Perhaps that actually is what you're seeing?).
That's probably the disadvantage of using a list to avoid an IndexError (sorry 
Steven; I would have just caught the IndexError, or have an 'if i<  2: 
continue' statement in there.)


Yes of course :) That's a typo on my part.  I came up with the following 
which I think works as well?


import random
reeks = []
for i in range(60):
  temp = [random.randint(1, 6)]
  while reeks[-2:-1] == reeks[-1:] == temp:
temp = [random.randint(1, 6)]
if reeks.count(temp[0]) >= 10:
  temp = [random.randint(1, 6)]
  reeks.extend(temp)
print reeks



As suggestion for avoiding occurrences of>  10 times: use a dict, where the random 
numbers become the keys and you add +1 every time to d[temp]. Checking if d[temp]> 
 10 is then very easy.


I'll check dicts as well.  Thank you! :)
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] box drawing characters

2010-08-18 Thread Bill Allen
On Wed, Aug 18, 2010 at 3:15 AM, Alan Gauld wrote:

>
> "Bill Allen"  wrote
>
>> I want to be able to create some user interfaces, similar to what you see
>>>
>> in console based programs like Midnight Commander or other TUI type
>> programs
>> that use box drawing characters to create a user interfaces for input and
>> display of information.
>>
>
> This is OK and you can use curses on Linux/Unix systems and something
> like Conio on Windows, but
>
>
>  However, I do want them to be portable to most
>> platforms that Python will run on.
>>
>
> This is much more difficult. Terminals are non portable and neither
> of the libraries above work on the other system. You would probably
> have to build your UI twice. This is one area where real GUIs work
> better than a terminal.
>
> This is good to know.   For my purposes either will be an acceptable option

for me as I am very much in learning mode.   In the endgame, I can always
do an OS detection and load whichever code is appropriate as the duplication
does not bother me much.

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


Re: [Tutor] box drawing characters

2010-08-18 Thread Ewald Horn
Hi Bill,

have you given UniCurses a spin?

See http://pyunicurses.sourceforge.net/ for more information.

I recall finding it online, so it might be worth investigating.

Good luck.

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


Re: [Tutor] Need help understanding output...

2010-08-18 Thread Dave Angel



Laurens Vets wrote:



Yes of course :) That's a typo on my part.  I came up with the 
following which I think works as well?


import random
reeks = []
for i in range(60):
  temp = [random.randint(1, 6)]
  while reeks[-2:-1] == reeks[-1:] == temp:
temp = [random.randint(1, 6)]
if reeks.count(temp[0]) >= 10:
  temp = [random.randint(1, 6)]
  reeks.extend(temp)
print reeks


Without actually trying it, I think you've got a problem in the 
reeks.count code.


   You only check the count if you've already match temp with the last 
two list items.



I'd add the reeks.count logic as a compound "or" in the test of the 
while loop.  And since that makes a pretty long line, I might then make 
a separate function of the "is temp a valid item to add to this list"


*untested* :

def is_valid(item, thelist):
 if len(list) >1 and (thelist[-2] == thelist[-1] == item):
   return False
 if thelist.count(item) >= 10:
return False
 return True

import random
reeks = []
for i in range(60):
   temp = random.randint(1, 6)
   while not is_valid(temp):
   temp = random.randint(1, 6)
   reeks.append(temp)
print reeks


I also fixed the indentation to 4, and made temp a scalar instead of a list.

DaveA

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


[Tutor] Multiple file open

2010-08-18 Thread nitin chandra
Hi All,

@ Alan - Thank you. Your suggestion worked. ( read 2 input file and 1
output file).

I am on Python 2.6

I need to process two sets of 96 files and prepare equal no. of sets
of 96 files. The first set of 96 files have a formula A and second set
of 96 files have formula B. Formula A is derived as follows - from 2
input files in 2 different paths, read 4 columns each, (add, sub, div,
etc) and write it to a One output file in 3rd different path.(This
part is done, but will require me to run it 96 times, you can imagine
my plight).

Change the formula and make another PY script and then run it . 96
times . again ...

So I was thinking like this

create a CSV file with a list of files for formula A and create a 2nd
CSV file with a list of files for formula B.
as follows :

column 1 will have full path of FIRST INPUT file to pass to the
existing Program.
column 2 will have full path of SECOND INPUT file to pass to the
existing Program.
column 3 will have full path of THIRD OUTPUT file to pass to the
existing Program.

a switch statement to choose between Formula A or Formula B or Exit.


Please guide with the syntax.

below is the existing program with Formula A (Mean). Formula B will be
Extrapolation,
also I have not been able to do justice to 'except IOError:' part.The
program ends with an error.

Thanks In Advance.

Nitin


import sys,os, fileinput


file11 = raw_input('Enter PR1 File name :')
fp1 = open(file11,'r')

file12 = raw_input('Enter PR3 File Name :')
fp2 = open(file12,'r')

file3 = raw_input('Enter PR2 / PR4 OUTPUT File Name :')
fp3 = open(file3,'w')

while True:
   try:
   line1A = fp1.readline()
   line1B = fp2.readline()
   line1 = line1A.split(",")
   col1 = line1[0]
   col2 = line1[1]
   col3 = line1[2]
   col4 = line1[3]
   col5 = line1[20]
   col6 = line1[21]
   col7 = line1[22]
   line2 = line1B.split(",")
   col8 = line2[1]
   col9 = line2[2]
   col10 = line2[3]
   col11 = line2[20]
   col12 = line2[21]
   col13 = line2[22]
   # calculation of PR2 as per formula
   #(A+B)/2 , ie. Mean of the two values
   col14 = ( (float(col2)) + (float(col8)) / 2)
   col15 = ( (float(col3)) + (float(col9)) / 2)
   col16 = ( (float(col4)) + (float(col10)) / 2)
   col17 = ( (float(col5)) + (float(col11)) / 2)
   col18 = ( (float(col6)) + (float(col12)) / 2)
   col19 = ( (float(col7)) + (float(col13)) / 2)

   print col1,col14,col15,col16,col17,col18,col19
   str3 = '%s,%s,%s,%s,%s,%s,%s\n' %
(col1,col14,col15,col16,col17,col18,col19)
   fp3.write(str3)

   except IOError:
 print 'Error in One of the Input Files'

fp1.close()
fp2.close()
fp3.close()
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] box drawing characters

2010-08-18 Thread Alan Gauld

Looks interesting, a new one for me, thanks for posting.

Alan G.

"Ewald Horn"  wrote in message 
news:aanlktinmkzyxbd0t7rldyexhbanw1tnfzac5z2gee...@mail.gmail.com...

Hi Bill,

have you given UniCurses a spin?

See http://pyunicurses.sourceforge.net/ for more information.

I recall finding it online, so it might be worth investigating.

Good luck.

Regards
Ewald
___
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] box drawing characters

2010-08-18 Thread Bill Allen
On Wed, Aug 18, 2010 at 12:36 PM, Alan Gauld wrote:

> Looks interesting, a new one for me, thanks for posting.
>
> Alan G.
>
> "Ewald Horn"  wrote in message
> news:aanlktinmkzyxbd0t7rldyexhbanw1tnfzac5z2gee...@mail.gmail.com...
>
>  Hi Bill,
>>
>> have you given UniCurses a spin?
>>
>> See http://pyunicurses.sourceforge.net/ for more information.
>>
>>
> This does look very interesting and the documentation seems to reflect a
product that is quite complete.   I will definitely give it a try.

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


Re: [Tutor] Need help understanding output...

2010-08-18 Thread Steven D'Aprano
On Wed, 18 Aug 2010 09:21:51 pm Laurens Vets wrote:
> On 8/12/2010 1:26 AM, Steven D'Aprano wrote:
> > On Thu, 12 Aug 2010 07:04:15 am Laurens Vets wrote:
> >> I need to generate a list of 30 numbers randomly chosen from 1, 2,
> >> 3, 4, 5&  6. However, I cannot have more than 2 numbers which are
> >> the same next to each other.
[...]
> Thank you all for your help! I've added another condition to this
> program, namely, in a range of 60, each 'random' number can only
> occur 10 times. I came up with the following:


The more of these restrictions you add, the less random the sequence is. 
A truly random (that is, uniformly random) sequence should come up with 
30 identical numbers occasionally. *Very* occasionally, to be sure, but 
it still should be possible.

If what you need is a sequence of numbers which is only slightly random, 
then that's fine. But I don't fully understand what your aim is here.

Anyway, here's my take on generating 30 semi-random integers from 1 to 6 
where there are no more than 10 of each number and no more than two in 
a row of anything.

Completely untested:

def make_counts(total=30):
counts = {}
t = total
for i in range(1, 7):
n = random.randint(0, min(t, 10))
counts[i] = n
t -= n
assert sum(counts.values) == total  # Is this guaranteed?
return counts


def make_seq():
result = []
counts = make_counts(30)
for i in range(30):
t = random.choose(counts.keys())
while [t] != result[-2:]
if len(counts) == 1 and counts.keys() == result[-2:]:
# Stuck!
raise ValueError
t = random.choose(counts.keys())
result.append(t)
counts[t] -= 1
if counts[t] == 0:
del counts[t]
return result


I'm a bit dubious about the assertion in make_counts... I think I need 
to be a bit more clever about generating the counts to guarantee that 
there will be exactly 30 in total. At the moment it only guarantees no 
more than 30.

Note also the test inside the while loop in make_seq. Given your 
additional restriction, it might happen that you have a sequence like 
[blah blah... 3, 3] and 3 is the only remaining key in count. In that 
case, the while loop will never terminate.

Another alternative is not to try to be clever at all. Instead, use a 
nice simple rejection method:

* generate a list of thirty random numbers
* reject it if it has 3 numbers in a row, or more than 10 of any number
* if rejected, start again with a new random list




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


Re: [Tutor] Multiple file open

2010-08-18 Thread Sander Sweers
On 18 August 2010 17:14, nitin chandra  wrote:
> I am on Python 2.6
>


> Please guide with the syntax.

All beginners tutorials on the web teach the syntax of python.. I am
unsure what your questions is.

> below is the existing program with Formula A (Mean). Formula B will be
> Extrapolation,
> also I have not been able to do justice to 'except IOError:' part.The

It has many errors which need fixing but..

> program ends with an error.

And if you get an error always make sure you provide it to this list.
People on this list can not read minds ;-).

> 
> import sys,os, fileinput
>
>
> file11 = raw_input('Enter PR1 File name :')
> fp1 = open(file11,'r')
>
> file12 = raw_input('Enter PR3 File Name :')
> fp2 = open(file12,'r')
>
> file3 = raw_input('Enter PR2 / PR4 OUTPUT File Name :')
> fp3 = open(file3,'w')

What happens when you enter a wrong filename? It will raise IOError
the moment you use open(). Below is 1 example of what you might want
tot do.

file11 = raw_input('Enter PR1 File name :')
try:
fp1 = open(ffile11, 'r')
except IOError:
   sys.exit('Could not open file: %s' % file11)

> while True:
>   try:
>       line1A = fp1.readline()

You can read and split the line in 1 go.
 line1 = fp1.readline().split(",")

>       line1B = fp2.readline()
>       line1 = line1A.split(",")

You can now remove the above, see earlier comment.

>       col1 = line1[0]
>       col2 = line1[1]
>       col3 = line1[2]
>       col4 = line1[3]
>       col5 = line1[20]
>       col6 = line1[21]
>       col7 = line1[22]
>       line2 = line1B.split(",")
>       col8 = line2[1]
>       col9 = line2[2]
>       col10 = line2[3]
>       col11 = line2[20]
>       col12 = line2[21]
>       col13 = line2[22]

Above you create a list "line1 = fp1.readline().split(",")". Lists are
mutable so you can add, remove and insert new items. You can also
access the items in the list by index. You can even add 2 lists to
make a new list, eg: .

>>> [1,2,3,4,5] + [6,7,8,9,0]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 0]

Now try and apply this to all your col variables.

>       # calculation of PR2 as per formula
>       #(A+B)/2 , ie. Mean of the two values
>       col14 = ( (float(col2)) + (float(col8)) / 2)

You can write this as:
  col14 = (float(line1[1])) + (float(line2[1])) / 2)

And avoid creating all these col variables. But what would happen if
col2/line1[1] has a value of "I am not a number"? Do note the comment
below on the actual calculation.

>       col15 = ( (float(col3)) + (float(col9)) / 2)

>       col19 = ( (float(col7)) + (float(col13)) / 2)

Your "mean" calculation is wrong (hint, division has precedence over adding).

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


[Tutor] Immutable objects

2010-08-18 Thread Nitin Das
Hello,

 Can somebody help me how to make immutable objects in python. I have
seen overriding the __new__, __setattr__ methods.. but not comfortable with
how to use them to make the object immutable.

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


Re: [Tutor] Immutable objects

2010-08-18 Thread Nitin Das
class mymut(object):

  def __setattr__(self,k,v):
  if hasattr(self,k):
  if self.__dict__.get(k) == None:
  self.__dict__[k] = v
  else:
  raise TypeError("Cant Modify Attribute Value")
  else:
  raise TypeError("Immutable Object")


class mm(mymut):
x = ''
y = ''
def __init__(self,x,y):
self.x = x
self.y = y



p = mm(10,11)
print p.x
print p.y


I have created this immutable object.Is there any other better
implementation?

thanks
-- nitin




On Thu, Aug 19, 2010 at 8:54 AM, Nitin Das  wrote:

> Hello,
>
>  Can somebody help me how to make immutable objects in python. I have
> seen overriding the __new__, __setattr__ methods.. but not comfortable with
> how to use them to make the object immutable.
>
> thanks in advance
> --nitin
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor