Re: [Tutor] Running .py files in shell

2011-09-22 Thread Steven D'Aprano

Robert Layne wrote:

Well everybody, sorry for the incomplete sentences
and overall poor English but I wanted to make this 
simple to read and understand for someone who 
is completely inexperienced in any sort of programming,


Generally speaking, incomplete sentences and overall poor English make 
things HARDER to read and understand rather than easier.


Or to put it another way:

Generally speaking, incomplete overall poor make things HARDER read 
understand than easier.




as I am (very first day messing with this stuff, e.g., 
terminal).  This is the result of hours of Googling that
was all done in one day.  Perhaps someone who is 
familiar with the commands below (in bold) wouldn’t 


Many of your readers -- including me -- prefer plain text email rather 
than HTML (what Outlook wrongly calls "rich text"), for various reasons 
including security. So you should not assume that colours and bold text 
will be coloured or bold. If you want to emphasis text, writing it like 
*this* is a good way.


This *especially* holds true for programmers, who tend to be very 
suspicious of fancy colourful fonts and dancing paperclips and prefer 
good old plain text that you could read over telnet using a 28K modem to 
a computer in Siberia over a flaky link at 3 in the morning.




mind explaining what exactly is taking place.  Additionally,
this was all done in terminal on a MacBook Pro 
running Mac OS Lion.


Unfortunately, I haven't used a Mac since about 1999 or thereabouts, so 
I can't answer any Mac specific questions.


However, I will say one thing: you seem to have made a really 
complicated job out of something as simple as "be able to run Python 
programs from the shell".


For starters, I'm pretty sure Mac OS X comes with Python automatically. 
Perhaps not the most recent version, but I'm sure it will be there. Just 
try running "python" from the shell, and it should Just Work.


If you want to install the most recent version, you shouldn't need to 
install pygame, then uninstall pygame. It shouldn't take eight steps to 
install the latest version of Python! (Even installing from source code 
under Linux, it only takes five: download, extract, configure, make, 
install.)


My suggestion is you try something like the Mac installer for ActivePython:

http://www.activestate.com/activepython/downloads

The instructions here are pretty old, but they should give you some hints:

http://diveintopython.org/installing_python/macosx.html

Or just use the Mac installer from here:

http://www.python.org/download/




--
Steven

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


Re: [Tutor] How it is better than java

2011-09-22 Thread Steven D'Aprano

Mac Ryan wrote:

On Tue, 20 Sep 2011 10:27:12 +1000
Steven D'Aprano  wrote:



There are three misunderstandings with that statement.
[snip]
There's also JPype, which claims to give full access to Java
libraries in Python.


Now: this was one of the best write-ups on the subject I read. Concise,
clear, documented. Nice way to start the day! :o



I'm glad it was helpful and interesting. Thank you to everyone for the 
kind words.





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


[Tutor] range question

2011-09-22 Thread Joel Knoll

Given a range of integers (1,n), how might I go about printing them in the 
following patterns:
1 2 3 4 ... n2 3 4 5 ... n 13 4 5 6 ... n 1 2 
etc., e.g. for a "magic square". So that for the range (1,5) for example I 
would get
1 2 3 42 3 4 13 4 1 24 1 2 3
I just cannot figure out how to make the sequence "start over" within a row, 
i.e. to go from 4 down to 1 in this example. 
I have been grappling with this problem for 2.5 days and have gotten nowhere!   
  ___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] range question

2011-09-22 Thread James Reynolds
On Thu, Sep 22, 2011 at 10:27 AM, Joel Knoll  wrote:

>  Given a range of integers (1,n), how might I go about printing them in the
> following patterns:
>
> 1 2 3 4 ... n
> 2 3 4 5 ... n 1
> 3 4 5 6 ... n 1 2
>
> etc., e.g. for a "magic square". So that for the range (1,5) for example I
> would get
>
> 1 2 3 4
> 2 3 4 1
> 3 4 1 2
> 4 1 2 3
>
> I just cannot figure out how to make the sequence "start over" within a
> row, i.e. to go from 4 down to 1 in this example.
>
> I have been grappling with this problem for 2.5 days and have gotten
> nowhere!
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>

I would probably do something like this:

a = [1,2,3,4]
> print a
> b = a.pop(0)
> a.append(b)
> print a


Probably an easier way of doing it though.

If you wanted to do that four times:

for item in a:
> print a
> b = a.pop(0)
> a.append(b)




You just need to think of it as a register and shifting bits left or right
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] range question

2011-09-22 Thread Dave Angel

On 09/22/2011 10:27 AM, Joel Knoll wrote:

Given a range of integers (1,n), how might I go about printing them in the 
following patterns:
1 2 3 4 ... n2 3 4 5 ... n 13 4 5 6 ... n 1 2
etc., e.g. for a "magic square". So that for the range (1,5) for example I 
would get
1 2 3 42 3 4 13 4 1 24 1 2 3
I just cannot figure out how to make the sequence "start over" within a row, 
i.e. to go from 4 down to 1 in this example.
I have been grappling with this problem for 2.5 days and have gotten nowhere!   



___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Seems like the easiest way would be to duplicate the range once (so you 
have a list twice as long), and then use various slices of it.


x = list(range(1, 5))   #could omit the list() function in python 2.x
x2 = x+x

for the nth row, use
   row = x2[n:n+n]



--

DaveA

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


[Tutor] range question

2011-09-22 Thread Hugo Arts
forgot to forward to list:

From: Hugo Arts 
Date: Thu, Sep 22, 2011 at 4:42 PM
Subject: Re: [Tutor] range question
To: d...@davea.name


On Thu, Sep 22, 2011 at 4:37 PM, Dave Angel  wrote:
> On 09/22/2011 10:27 AM, Joel Knoll wrote:
>
> Given a range of integers (1,n), how might I go about printing them in the
> following patterns:
> 1 2 3 4 ... n2 3 4 5 ... n 13 4 5 6 ... n 1 2
> etc., e.g. for a "magic square". So that for the range (1,5) for example I
> would get
> 1 2 3 42 3 4 13 4 1 24 1 2 3
> I just cannot figure out how to make the sequence "start over" within a row,
> i.e. to go from 4 down to 1 in this example.
> I have been grappling with this problem for 2.5 days and have gotten
> nowhere!
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
> Seems like the easiest way would be to duplicate the range once (so you have
> a list twice as long), and then use various slices of it.
>
> x = list(range(1, 5))   #could omit the list() function in python 2.x
> x2 = x+x
>
> for the nth row, use
>    row = x2[n:n+n]
>

Surely you meant to type:

x = list(range(1, 6))
x2 = x + x
row  = x[n:n+len(x)]
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] range question

2011-09-22 Thread Joel Goldstick
On Thu, Sep 22, 2011 at 10:37 AM, Dave Angel  wrote:

> **
> On 09/22/2011 10:27 AM, Joel Knoll wrote:
>
> Given a range of integers (1,n), how might I go about printing them in the 
> following patterns:
> 1 2 3 4 ... n2 3 4 5 ... n 13 4 5 6 ... n 1 2
> etc., e.g. for a "magic square". So that for the range (1,5) for example I 
> would get
> 1 2 3 42 3 4 13 4 1 24 1 2 3
> I just cannot figure out how to make the sequence "start over" within a row, 
> i.e. to go from 4 down to 1 in this example.
> I have been grappling with this problem for 2.5 days and have gotten nowhere! 
> 
>
>
> ___
> Tutor maillist  -  Tutor@python.org
>
> To unsubscribe or change subscription 
> options:http://mail.python.org/mailman/listinfo/tutor
>
>  Seems like the easiest way would be to duplicate the range once (so you
> have a list twice as long), and then use various slices of it.
>
> x = list(range(1, 5))   #could omit the list() function in python 2.x
> x2 = x+x
>
> for the nth row, use
>row = x2[n:n+n]
>
>
>
> --
>
> DaveA
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>
I did it like this:

The trick when cycling through numbers is to use the modulo operator (%)
which gives the integer remainder after division


#!/usr/bin/env python


""" Print magic square give range n like so:
If n is 5

1234
2341
3412
4123
"""

n = 5

for row in range(n):
for col in range(n):
print (row + col) % n + 1,
print

-- 

1 2 3 4 5
2 3 4 5 1
3 4 5 1 2
4 5 1 2 3
5 1 2 3 4

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


Re: [Tutor] range question

2011-09-22 Thread Dave Angel

On 09/22/2011 10:43 AM, Hugo Arts wrote:

forgot to forward to list:

From: Hugo Arts
Date: Thu, Sep 22, 2011 at 4:42 PM
Subject: Re: [Tutor] range question
To: d...@davea.name


On Thu, Sep 22, 2011 at 4:37 PM, Dave Angel  wrote:

On 09/22/2011 10:27 AM, Joel Knoll wrote:

Given a range of integers (1,n), how might I go about printing them in the
following patterns:
1 2 3 4 ... n2 3 4 5 ... n 13 4 5 6 ... n 1 2
etc., e.g. for a "magic square". So that for the range (1,5) for example I
would get
1 2 3 42 3 4 13 4 1 24 1 2 3
I just cannot figure out how to make the sequence "start over" within a row,
i.e. to go from 4 down to 1 in this example.
I have been grappling with this problem for 2.5 days and have gotten
nowhere!

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

Seems like the easiest way would be to duplicate the range once (so you have
a list twice as long), and then use various slices of it.

x = list(range(1, 5))   #could omit the list() function in python 2.x
x2 = x+x

for the nth row, use
row = x2[n:n+n]


Surely you meant to type:

x = list(range(1, 6))
x2 = x + x
row  = x[n:n+len(x)]



The second correction is important, the first one wrong.  The OP's 
example was looking for the numbers 1 through 4, so range(1,5) was correct.

--

DaveA

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


Re: [Tutor] range question

2011-09-22 Thread Hugo Arts
On Thu, Sep 22, 2011 at 4:56 PM, Dave Angel  wrote:
>
> The second correction is important, the first one wrong.  The OP's example
> was looking for the numbers 1 through 4, so range(1,5) was correct.
> --
>
> DaveA
>
>

Ah, apologies. I misread the OP
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] range question

2011-09-22 Thread Steven D'Aprano

Joel Knoll wrote:

Given a range of integers (1,n), how might I go about printing them in the 
following patterns:
1 2 3 4 ... n2 3 4 5 ... n 13 4 5 6 ... n 1 2 
etc., e.g. for a "magic square". So that for the range (1,5) for example I would get

1 2 3 42 3 4 13 4 1 24 1 2 3



I'm not sure what you want, because the formatting is all broken in 
Thunderbird. Your "magic square" looks more like a straight line.


I'm going to take a wild stab in the dark and *guess* that you want 
something like this:


1 2 3 4
2 3 4 1
3 4 1 2
4 1 2 3

Look at the pattern: each row is the same as the previous row, except 
the first item is moved to the end. You can move an item from the front 
to the end with pop() to delete it, and append() to re-add it.


There are the same number of rows as columns. Putting this together:


n = 4  # number of columns
row = range(1, n+1)
for row_number in range(n):
# print the row without commas and []
for item in row:
print item,  # note the comma at the end
print  # start a new line
# Pop the first item from the list, then add it to the end.
x = row.pop(0)
row.append(x)



--
Steven

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


Re: [Tutor] range question

2011-09-22 Thread James Reynolds
On Thu, Sep 22, 2011 at 11:08 AM, Steven D'Aprano wrote:

> Joel Knoll wrote:
>
>> Given a range of integers (1,n), how might I go about printing them in the
>> following patterns:
>> 1 2 3 4 ... n2 3 4 5 ... n 13 4 5 6 ... n 1 2 etc., e.g. for a "magic
>> square". So that for the range (1,5) for example I would get
>>
>> 1 2 3 42 3 4 13 4 1 24 1 2 3
>>
>
>
> I'm not sure what you want, because the formatting is all broken in
> Thunderbird. Your "magic square" looks more like a straight line.
>
> I'm going to take a wild stab in the dark and *guess* that you want
> something like this:
>
>
> 1 2 3 4
> 2 3 4 1
> 3 4 1 2
> 4 1 2 3
>
> Look at the pattern: each row is the same as the previous row, except the
> first item is moved to the end. You can move an item from the front to the
> end with pop() to delete it, and append() to re-add it.
>
> There are the same number of rows as columns. Putting this together:
>
>
> n = 4  # number of columns
> row = range(1, n+1)
> for row_number in range(n):
># print the row without commas and []
>for item in row:
>print item,  # note the comma at the end
>print  # start a new line
># Pop the first item from the list, then add it to the end.
>x = row.pop(0)
>row.append(x)
>
>
>
> --
> Steven
>
>
> __**_
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/**mailman/listinfo/tutor
>


An easier way to print without the comma, assuming 2.6 or 2.7 is to add
this:

 from __future__ import print_function

and then print like this:

print(*a)

or if you are already using python 3.0+ you just print like that without the
import.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Help!

2011-09-22 Thread Joseph Shakespeare
Hello,

I am new Python (about 2 weeks) and need some help. I am making a rock paper
scissors game that a user can play with the computer by using a while loop
and if elif else statements. It needs to keep score of the amount of losses
and wins, and give the user the option to play again after each round. I've
tried so many different options and I don't know what else to try. My
problem is making the program loop back around when the user selects y(yes)
to play again and defining the variables at the beginning. Here's my
program:

import random
print"Welcome to Rock,Paper, Scissors! This is a game of chance; the
computer randomly picks one of three throws."
print""
print"Rock beats Scissors, but is beaten by Paper."
print"Scissors beat Paper, but are beaten by Rock."
print"Paper beats Rock, but is beaten by Scissors."
print""
print"r for Rock"
print"s for Scissors"
wins=0
loses=0
print"p for Paper"
player=raw_input("Please pick your throw: (r,s,p):")
computer= random.choice(['r','s','p'])
print "Computer throw:", computer
y="something"
play=y
while play==y:
if player=='r':
if computer== 'r':
print "Tie! Throw again."
elif computer=='s':
print "You win! r beats s"
wins=wins+1
elif computer == 'p':
print "You lose! p beats r"
loses=loses+1
else:
pass
elif player=='s':
if computer== 's':
print "Tie! Throw again."
elif computer=='p':
print "You win! s beats p"
wins=wins+1
elif computer == 'r':
print "You lose! r beats s"
loses=loses+1
else:
pass
elif player=='p':
if computer== 'p':
print "Tie! Throw again."
elif computer=='r':
print "You win! p beats r"
wins=wins+1
elif computer == 's':
print "You lose! s beats p"
loses=loses+1
else:
pass
else:
print "Invalid entry"
print""
print"Game Summary"
print"Wins:", wins
print"Loses:",loses
play=raw_input("Play again? y (yes) or n (no):"
print"Thanks for playing!"



Thanks!

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


Re: [Tutor] Help!

2011-09-22 Thread Prasad, Ramit
From: tutor-bounces+ramit.prasad=jpmorgan@python.org 
[mailto:tutor-bounces+ramit.prasad=jpmorgan@python.org] On Behalf Of Joseph 
Shakespeare
Sent: Thursday, September 22, 2011 12:05 PM
To: tutor@python.org
Subject: [Tutor] Help!

Hello,

I am new Python (about 2 weeks) and need some help. I am making a rock paper 
scissors game that a user can play with the computer by using a while loop and 
if elif else statements. It needs to keep score of the amount of losses and 
wins, and give the user the option to play again after each round. I've tried 
so many different options and I don't know what else to try. My problem is 
making the program loop back around when the user selects y(yes) to play again 
and defining the variables at the beginning. Here's my program:

import random
print"Welcome to Rock,Paper, Scissors! This is a game of chance; the computer 
randomly picks one of three throws."
print""
print"Rock beats Scissors, but is beaten by Paper."
print"Scissors beat Paper, but are beaten by Rock."
print"Paper beats Rock, but is beaten by Scissors."
print""
print"r for Rock"
print"s for Scissors"
wins=0
loses=0
print"p for Paper"
player=raw_input("Please pick your throw: (r,s,p):")
computer= random.choice(['r','s','p'])
print "Computer throw:", computer
y="something"
play=y
while play==y:
    if player=='r':
        if computer== 'r':
            print "Tie! Throw again."
        elif computer=='s':
            print "You win! r beats s"
            wins=wins+1
        elif computer == 'p':
            print "You lose! p beats r"
            loses=loses+1
        else:
            pass
    elif player=='s':
        if computer== 's':
            print "Tie! Throw again."
        elif computer=='p':
            print "You win! s beats p"
            wins=wins+1
        elif computer == 'r':
            print "You lose! r beats s"
            loses=loses+1
        else:
            pass
    elif player=='p':
            if computer== 'p':
                print "Tie! Throw again."
            elif computer=='r':
                print "You win! p beats r"
                wins=wins+1
            elif computer == 's':
                print "You lose! s beats p"
                loses=loses+1
            else:
                pass
    else:
        print "Invalid entry"
    print""
    print"Game Summary"
    print"Wins:", wins
    print"Loses:",loses
    play=raw_input("Play again? y (yes) or n (no):"
print"Thanks for playing!"



Thanks!

Joey
===

Change 
> y="something"
TO :
> y="y"

Although, I would probably do something like
y=("y","yes")
play="y"
while play in y:



Ramit


Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
712 Main Street | Houston, TX 77002
work phone: 713 - 216 - 5423



This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help!

2011-09-22 Thread xDog Walker

Move the following two lines to immediately follow the while.

 player=raw_input("Please pick your throw: (r,s,p):")
 computer= random.choice(['r','s','p'])

-- 
I have seen the future and I am not in it.

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


Re: [Tutor] paper scissors

2011-09-22 Thread bob gailer

On 9/22/2011 1:04 PM, Joseph Shakespeare wrote:

Hello,


Hi - please use a meaningful subject line - I've changed it this time


I am new Python (about 2 weeks) and need some help. I am making a rock 
paper scissors game that a user can play with the computer by using a 
while loop and if elif else statements. It needs to keep score of the 
amount of losses and wins, and give the user the option to play again 
after each round. I've tried so many different options and I don't 
know what else to try. My problem is making the program loop back 
around when the user selects y(yes) to play again and defining the 
variables at the beginning. Here's my program:


I've taken the liberty to extensively edit (and simplify) your program. 
Cut 60 lines to 34. Study it - there are a lot of useful ideas in it. 
Feel free to ask questions.


The following 3 llnes will NOT get you what you want. You appear to be 
confusing variable names with strings.

y="something"
play=y
while play==y:


The while condition is not true - the program will not run.

The proper initialization is:

play = "y"

However I've eliminated the need for even that.

import random
# Use a triple-quoted string rather than a bunch of print statements.
print """Welcome to Rock,Paper, Scissors! This is a game of chance.
The computer randomly picks one of three throws.
Rock beats Scissors, but is beaten by Paper.
Scissors beat Paper, but are beaten by Rock.
Paper beats Rock, but is beaten by Scissors.
You enter:
r for Rock
s for Scissors
p for Paper
q to Quit'"""
wins = loses = 0
while True: # "endless" loop - exited by break statement
player = raw_input("Please pick your throw: (r, s, p, or q ):")
if player == "q":
break # exits the loop
elif player not in "rps": # check for valid entry
print "Invalid entry"
else:
computer= random.choice("rsp") # no need for a list - a string 
is a sequence

print "Computer throw:", computer
if player == computer: # testing various conditiions cam be 
greatly simplified

print "Tie! Throw again."
elif player + computer in ["rs", "sp", "pr"]:
print "You win! " + player + " beats " + computer
wins += 1 # simpler than wins = wins + 1
else:
print "You lose! " + computer + " beats " + player
loses +=1
print """Game Summary
Wins: %s
Loses:" %s""" % (wins,loses) # using % formatting and triple quoted string
print"Thanks for playing!"


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


[Tutor] Need a bump in the right direction (network programming)

2011-09-22 Thread Cheeyung
I'm creating a mobile application and I'm using python for a desktop 
server. However, I don't have access to a static IP on the desktop, but 
do have a website. Is it possible to connect from mobile -> http website 
-> desktop server and back?


What kind of (python) libraries/functions am I looking for?

As you can see, I'm completely clueless about where to begin. If someone 
could recommend a resource to learn network programming, that would be 
great too!


Thanks!

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