Any Python programers here?

Some of us love those electrons just a little too much
Ddrak
Save a Koala, deport an Australian
Posts: 17516
Joined: Thu Jan 02, 2003 3:00 pm
Location: Straya mate!
Contact:

Re: Any Python programers here?

Post by Ddrak »

Python prints a newline unless you end the print statement with a comma, so you end up with:

Code: Select all

for x in card:
    for s in suit:
       print x+suit[s],
    print
(the second print makes the newline after each set of suits)

Dd
Image
Kulaf
Soverign Grand Postmaster General
Posts: 7183
Joined: Mon Nov 08, 2004 3:06 am

Re: Any Python programers here?

Post by Kulaf »

Yeah I don't know the language so I just wanted to make it work. And he's learning so I didn't want it to look like a professional programer wrote it.
Bahd Zoolander
Grand Inspector Inquisitor Commander
Posts: 2636
Joined: Fri Dec 20, 2002 9:42 pm

Re: Any Python programers here?

Post by Bahd Zoolander »

The C++ programmer in me thought that the for loop would give an index, but it gives the actual value, so this works:

Code: Select all

card = ('A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K')
suit = ('c', 'd', 'h', 's')

for x in card:
    for s in suit:
        print x + s,
    print
At least it got me to install python again. :)

So, why does it add a space after each print? I assumed it wouldn't. *shrug*
Bahd Zoolander - Transcendent - On Vacation
Ddrak
Save a Koala, deport an Australian
Posts: 17516
Joined: Thu Jan 02, 2003 3:00 pm
Location: Straya mate!
Contact:

Re: Any Python programers here?

Post by Ddrak »

Python just does that: comma puts a space in. If you don't want the space you have to do your previous trick of concatenating strings.
Python Docs wrote:6.6. The print statement
print_stmt ::= "print" ([expression ("," expression)* [","]]
| ">>" expression [("," expression)+ [","]])
print evaluates each expression in turn and writes the resulting object to standard output (see below). If an object is not a string, it is first converted to a string using the rules for string conversions. The (resulting or original) string is then written. A space is written before each object is (converted and) written, unless the output system believes it is positioned at the beginning of a line. This is the case (1) when no characters have yet been written to standard output, (2) when the last character written to standard output is '\n', or (3) when the last write operation on standard output was not a print statement. (In some cases it may be functional to write an empty string to standard output for this reason.)

Note
Objects which act like file objects but which are not the built-in file objects often do not properly emulate this aspect of the file object’s behavior, so it is best not to rely on this.

A '\n' character is written at the end, unless the print statement ends with a comma. This is the only action if the statement contains just the keyword print.

Standard output is defined as the file object named stdout in the built-in module sys. If no such object exists, or if it does not have a write() method, a RuntimeError exception is raised.

print also has an extended form, defined by the second portion of the syntax described above. This form is sometimes referred to as “print chevron.” In this form, the first expression after the >> must evaluate to a “file-like” object, specifically an object that has a write() method as described above. With this extended form, the subsequent expressions are printed to this file object. If the first expression evaluates to None, then sys.stdout is used as the file for output.
btw, Klast, if you haven't installed ActiveState Python then you're making it hard on yourself.

Dd
Image
Klast Brell
Sublime Prince of teh Royal Sekrut Strat
Posts: 4315
Joined: Fri Dec 20, 2002 11:17 am
Location: Minneapolis MN

Re: Any Python programers here?

Post by Klast Brell »

Ddrak wrote:
btw, Klast, if you haven't installed then you're making it hard on yourself.

Dd
Never heard of ActiveState Python. I know some IDEs do a lot of helping you write code. But if it helps too much it might be a crutch that prevents me from learning the language at a deeper level. Would I be risking that if I used ActiveState Python?
"A few months ago, I told the American people I did not trade arms for hostages. My heart and best intentions still tell me that's true, but the facts and evidence tell me it is not." - Ronald Reagan 1987
Barious
Sublime Master Elect0rzed
Posts: 389
Joined: Wed Jan 01, 2003 3:30 am

Re: Any Python programers here?

Post by Barious »

If you're just looking for something that will assist, without being a full blown IDE, just get a decent text editor. My personal favorite is Textpad.

-Barious
User avatar
Taxious
Rum Guzzler
Posts: 5056
Joined: Fri Apr 18, 2003 10:16 am
Location: Denver, CO

Re: Any Python programers here?

Post by Taxious »

Barious wrote:If you're just looking for something that will assist, without being a full blown IDE, just get a decent text editor. My personal favorite is Textpad.
I really like Notepad++ but I've never used Textpad so I can't compare.
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Ddrak
Save a Koala, deport an Australian
Posts: 17516
Joined: Thu Jan 02, 2003 3:00 pm
Location: Straya mate!
Contact:

Re: Any Python programers here?

Post by Ddrak »

Klast Brell wrote:
Ddrak wrote:
btw, Klast, if you haven't installed then you're making it hard on yourself.

Dd
Never heard of ActiveState Python. I know some IDEs do a lot of helping you write code. But if it helps too much it might be a crutch that prevents me from learning the language at a deeper level. Would I be risking that if I used ActiveState Python?
Nothing. It's not really an IDE, just a nice UI and (more importantly) help system built around the standard Python install. It has some debugging ability and stepping but if you really wanted that type of thing I'd just say to get PyDev working inside Eclipse (what I use at work).

ActiveState Python
Eclipse
Update site for installing PyDev into Eclipse is http://pydev.sourceforge.net/updates/

and I prefer Notepad++ over Textpad personally, but it's somewhat of a religious war at work. ;)

Dd
Image
Klast Brell
Sublime Prince of teh Royal Sekrut Strat
Posts: 4315
Joined: Fri Dec 20, 2002 11:17 am
Location: Minneapolis MN

Re: Any Python programers here?

Post by Klast Brell »

I installed activestate. It comes with a user manual that explains the python language. But I can't seem to find the instructions for using the activestate application itself.
I use IDLE right now. Not very feature rich, but I know how to use the interface.
"A few months ago, I told the American people I did not trade arms for hostages. My heart and best intentions still tell me that's true, but the facts and evidence tell me it is not." - Ronald Reagan 1987
Ddrak
Save a Koala, deport an Australian
Posts: 17516
Joined: Thu Jan 02, 2003 3:00 pm
Location: Straya mate!
Contact:

Re: Any Python programers here?

Post by Ddrak »

Stick with IDLE then. It's not that bad really.

Dd
Image
Bahd Zoolander
Grand Inspector Inquisitor Commander
Posts: 2636
Joined: Fri Dec 20, 2002 9:42 pm

Re: Any Python programers here?

Post by Bahd Zoolander »

I like Notepad++, but if I need to open a big file I use Scite. It seems dumb to me that they both use Scintilla and Notepad++ can't open big files, but whatever.

ActiveState's IDE is called PythonWin. Right-click a .py file and pick Edit with PythonWin or run the shortcut in your start menu.
Bahd Zoolander - Transcendent - On Vacation
Klast Brell
Sublime Prince of teh Royal Sekrut Strat
Posts: 4315
Joined: Fri Dec 20, 2002 11:17 am
Location: Minneapolis MN

Re: Any Python programers here?

Post by Klast Brell »

I need to learn what step debugging and all that other stuff is.
"A few months ago, I told the American people I did not trade arms for hostages. My heart and best intentions still tell me that's true, but the facts and evidence tell me it is not." - Ronald Reagan 1987
User avatar
Taxious
Rum Guzzler
Posts: 5056
Joined: Fri Apr 18, 2003 10:16 am
Location: Denver, CO

Re: Any Python programers here?

Post by Taxious »

Klast Brell wrote:I need to learn what step debugging and all that other stuff is.
Debugging is pretty nice once you get into it. I was pretty against it during school (lazy) but now I'm starting to see the use of it very clearly.

Basically, you can see your program's state (variable values, etc...) at any point during execution. It's rurl nice when you have one of those "WTF IS WRONG WITH THIS CODE!?!" problems because you can easily/quickly narrow down where the issue is coming from.
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Klast Brell
Sublime Prince of teh Royal Sekrut Strat
Posts: 4315
Joined: Fri Dec 20, 2002 11:17 am
Location: Minneapolis MN

Re: Any Python programers here?

Post by Klast Brell »

I hit the wall on another program. Here is the code:

Code: Select all

##define a function named rate score
##if the score is less than 1000 return "less than 1K"
##if the score is 1000 to less than 10000 "1-10K"
##if the score is 10K or more return "more than 10K"
def rate_score(score=0):
    if score < 1000:
        answer =  "less than 1K"
elif score >= 1000 and score < 10000:
       answer = "1-10K"
    elif score >= 10000:
        answer =  "more than 10K"
    else:
        answer =  "Huh?:"
    return answer
            
#call the function several times and print the
#results for 50 5,000 50,000 and for passing it no value
scores=["",50,5000,50000]
for score in scores:
    print rate_score(score)

scores=["",50,5000,50000] the empty quote ("") returns "More than 10K". I set the default to zero so it should return "less than 1K"

Am i passing an empty value correctly? I don't want to pass it a zero. I want to pass it a null. I think it's reading the quote marks as a value in and off themselves. How do you add an empty value to a list of integers?
"A few months ago, I told the American people I did not trade arms for hostages. My heart and best intentions still tell me that's true, but the facts and evidence tell me it is not." - Ronald Reagan 1987
Kulaf
Soverign Grand Postmaster General
Posts: 7183
Joined: Mon Nov 08, 2004 3:06 am

Re: Any Python programers here?

Post by Kulaf »

Don't you have a keyword like "None" or "Null" that you can test for in your first if statement? Like:

if score < 1000 or score = None:
Ddrak
Save a Koala, deport an Australian
Posts: 17516
Joined: Thu Jan 02, 2003 3:00 pm
Location: Straya mate!
Contact:

Re: Any Python programers here?

Post by Ddrak »

What do you mean "the empty value"? Are you trying to get it to use the default parameter "=0" part?

"" = a string of length zero. On integer comparisons I'm pretty sure this evaluates to 0.
None = a value indicating the absence of a value. (kinda). None is less than every integer value.

To call the function in a way that triggers the default value of 0, you have to actually call it with no parameters, like "rate_score()".

Dd
Image
User avatar
Taxious
Rum Guzzler
Posts: 5056
Joined: Fri Apr 18, 2003 10:16 am
Location: Denver, CO

Re: Any Python programers here?

Post by Taxious »

Klast Brell wrote: Am i passing an empty value correctly? I don't want to pass it a zero. I want to pass it a null. I think it's reading the quote marks as a value in and off themselves. How do you add an empty value to a list of integers?
My first thought was that it was trying to multiply the ASCII values of the double quotes together, but that value only gives us 1,156 which isn't larger than 10k. Since you are comparing an empty string to an integer, maybe python interprets that string as the maximum value an int could hold (much larger than 10k)?

Just from reading the code, it looks like it should spit out "less than 1K", as the integer value of "" usually gets interpreted as 0.
Klast Brell wrote:I want to pass it a null.
A null value and an empty string are two very different things. Null basically says "I know what I am, but I don't have a value assigned to me yet" where an empty string is more "I know what I am, I have a value, but that value is empty."

Same thing with a null integer and the value 0, very different!
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Klast Brell
Sublime Prince of teh Royal Sekrut Strat
Posts: 4315
Joined: Fri Dec 20, 2002 11:17 am
Location: Minneapolis MN

Re: Any Python programers here?

Post by Klast Brell »

great. null, empty and zero are 3 different things. Thanks for breaking my brain a little bit more.
"A few months ago, I told the American people I did not trade arms for hostages. My heart and best intentions still tell me that's true, but the facts and evidence tell me it is not." - Ronald Reagan 1987
Post Reply