Re: [Tutor] help

2012-12-30 Thread Mike G
Hi Randy

> I am an older newbie teaching myself Python programming.
>

Me too :)

> My problem is I hear no system bell; the enter doesn't respond by quitting 
> the program;
> The problem with the program code the enter key hasn't worked in earlier 
> programs.
>
> I appreciate any advice I may recieve with this coding glitch.
>

I copied the code into a blank .py file and ran it from cmd in Windows
XP x86 using Python 273, it worked fine - including the beep. As well,
hitting "Enter" exited the program.

It sounds like (no pun intended) it may be how you're running the
program. I would use a py file and run it using cmd - holler if you
need help, you may if the path to Python isn't good to go.

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


[Tutor] another for loop question - latin square

2012-12-30 Thread Brandon Merritt
I am having trouble figuring out a solution after a couple hours now of
playing with the code. I'm trying to make a latin square using the code
below:

scaleorder = int(raw_input('Please enter a number for an n*n square: '))


topleft = int(raw_input('Please enter the top left number for the square:
'))

firstrow = range((topleft),scaleorder+1)

count = 0

while count < 8:
for i in firstrow:
print i
count += 1
firstrow[i+1]


---

It seemed like I could make the for loop work by doing something like this:

for i in firstrow:
print i, i+2


but  that obviously is not a solution either. Any ideas?

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


Re: [Tutor] another for loop question - latin square

2012-12-30 Thread Steven D'Aprano

On 31/12/12 10:59, Brandon Merritt wrote:

I am having trouble figuring out a solution after a couple hours now of
playing with the code. I'm trying to make a latin square using the code
below:

scaleorder = int(raw_input('Please enter a number for an n*n square: '))
topleft = int(raw_input('Please enter the top left number for the square: '))
firstrow = range((topleft),scaleorder+1)

count = 0
while count<  8:
 for i in firstrow:
 print i
 count += 1
 firstrow[i+1]


---

It seemed like I could make the for loop work by doing something like this:

for i in firstrow:
 print i, i+2


but  that obviously is not a solution either. Any ideas?



Absolutely none. I don't understand your question. What's a latin square?
Is it the same as a magic square? What do you mean, "make the for loop work"?
The for loop already works: it iterates over the range topleft to scaleorder
inclusive. You say "that obviously is not a solution", but a solution to
what? What is it supposed to do?

I think that you need to think a bit more carefully about what you are
trying to accomplish. Probably the most valuable piece of advice I ever
received was that *writing code* should be the last thing you do when
trying to solve a problem. When I have a tricky problem to accomplish,
I will sometimes spend hours designing my code with pencil and paper
before writing my first line of code.

Can YOU solve a latin square using pen and paper? If you can't, how do you
expect to write a program to do it?

Start by writing down the steps that you would take to make a latin square.
I suggest with starting with a fixed size, say, 5x5:

* pick a number for the top left corner, say, 3

* write down the first row: 3 ? ? ? ?
  [you need to come up with a rule for making the row]

* write down the second row: ? ? ? ? ?
  [again, you need to come up with a rule for making the next row]

... and so on for the other three rows. Now you have an algorithm for
making 5 x 5 latin squares.

Now you can write some code to do that!

Once that is working, you can start thinking about changing from fixed
5 x 5 squares to arbitrary N x N sizes.



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


Re: [Tutor] another for loop question - latin square

2012-12-30 Thread Dave Angel
On 12/30/2012 06:59 PM, Brandon Merritt wrote:
> I am having trouble

Please tell us what Python version you're targeting.  It looks like 2.7,
but you really should specify it for us.

Next, you should tell us your skill level;  are you experienced at
another language and learning Python, or what?  Is this latin square an
assignment, from a tutorial or book, or just for fun?  If from a
tutorial, have you been doing the earlier problems?  Have you completed
any other Python projects which were non-trivial?  Do you realize just
how tricky a latin square is to create?

>  figuring out a solution after a couple hours now of
> playing with the code. I'm trying to make a latin square

For other readers, an nxn latin square (not a magic square) has the same
n symbols in each row and in each column, with no duplicates in any row,
nor in any column.  For a 4x4 square, one solution might be

 A B C D
 B D A C
 C A D B
 D C B A


>  using the code
> below:
>
> scaleorder = int(raw_input('Please enter a number for an n*n square: '))
>
>
> topleft = int(raw_input('Please enter the top left number for the square:
> '))
>
> firstrow = range((topleft),scaleorder+1)

You do realize that this won't necessarily create a list of the
requested size?  Specifically, if the user didn't specify a top-left of
exactly 1, then the size will be wrong.

> count = 0
>
> while count < 8:
> for i in firstrow:
> print i
> count += 1
> firstrow[i+1]

Just what did you expect the last line to accomplish?  And what was the
8 supposed to mean?

>
> ---
>
> It seemed like I could make the for loop work by doing something like this:
>
> for i in firstrow:
> print i, i+2
>
>
> but  that obviously is not a solution either. Any ideas?
>
> Thanks,
> Brandon
>
>
>

Have you constructed a latin square by hand, of any size ?  (try 4 for
your first attempt)  Do you realize that you could build 3 lines and
then discover that there is no valid fourth line for those 3?

Have you learned how and why to build functions in your code?   I
maintain this problem is too tricky to do without at least factoring
each row into a function.



-- 

DaveA

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


Re: [Tutor] another for loop question - latin square

2012-12-30 Thread Mitya Sirenef

On 12/30/2012 06:59 PM, Brandon Merritt wrote:
I am having trouble figuring out a solution after a couple hours now 
of playing with the code. I'm trying to make a latin square using the 
code below:


scaleorder = int(raw_input('Please enter a number for an n*n square: '))


topleft = int(raw_input('Please enter the top left number for the 
square: '))


firstrow = range((topleft),scaleorder+1)

count = 0

while count < 8:
for i in firstrow:
print i
count += 1
firstrow[i+1]


---

It seemed like I could make the for loop work by doing something like 
this:


for i in firstrow:
print i, i+2



It's a bit hard to understand what you're trying to do, but if
my guess is anywhere close to truth, you might be trying
to make the first line of a latin square by incrementing
from a specified number. In this case, you should make a range
from topleft to scaleorder+topleft, and if you want the
top row randomized, you can use random.shuffle():

from random import shuffle

scaleorder = int(raw_input('Please enter a number for an n*n square: '))
topleft= int(raw_input('Please enter the top left number for the 
square: '))

firstrow   = range(topleft, scaleorder+topleft)
shuffle(firstrow)

print firstrow


Does this help?   -m



--
Lark's Tongue Guide to Python: http://lightbird.net/larks/

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


Re: [Tutor] another for loop question - latin square

2012-12-30 Thread Alan Gauld

On 31/12/12 00:27, Steven D'Aprano wrote:

On 31/12/12 10:59, Brandon Merritt wrote:

I am having trouble figuring out a solution after a couple hours now of
playing with the code. I'm trying to make a latin square using the code
below:


I totally agree with everything Steven said.
However there is one glaring mistake in your code



count = 0
while count<  8:
 for i in firstrow:
 print i
 count += 1
 firstrow[i+1]


What do you think that last row is doing?
Whatever it is, you are almost certainly wrong.
But eventually it is likely to throw an index error...


---

It seemed like I could make the for loop work by doing something like
this:

for i in firstrow:
 print i, i+2

but  that obviously is not a solution either. Any ideas?


It works perfectly. It prints out i and i+2 for every member of 
firstrow. Whether that's what you want to do is another matter.


As Steven said, try working out what you want to do first,
then write the code.

--
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