CEMETECH
Leading The Way To The Future
Login [Register]
Username:
Password:
Autologin:

Don't have an account? Register now to chat, post, use our tools, and much more.
Latest Headlines
Online Users
There are 111 users online: 6 members, 80 guests and 25 bots.
Members: chickendude, JosJuice, nsg, Spyro543.
Bots: Spinn3r (1), Magpie Crawler (4), Googlebot (19), MSN/Bing (1).
RSS & Social Media
SAX
You must log in to view the SAX chat widget
    » Goto page 1, 2, 3  Next
» View previous topic :: View next topic  
Author Message
MufinMcFlufin


Advanced Member


Joined: 29 Aug 2009
Posts: 252
Location: C:\Users\Admin\Desktop

Posted: 21 Aug 2011 11:55:04 pm    Post subject: Mufin's Python Questions

Hey, just started taking up Python, and was going to post any questions I might have about it here. Starting off, I'm trying to figure out how this code from the tutorial works, specifically the else on line 6. Does it only go to the else if nested for loop doesn't break, or how exactly would it go there?


Code:
for n in range(2, 10):
     for x in range(2, n):
         if n % x == 0:
             print n, 'equals', x, '*', n/x
             break
     else:
         # loop fell through without finding a factor
         print n, 'is a prime number'

_________________

MufinMcFlufin's quote of the week:
"Diapers and Politicians should both be changed often for the same reason."
-Anonymous
Back to top
christop


Power User


Joined: 09 Mar 2011
Posts: 385
Location: Arizona, USA

Posted: 22 Aug 2011 12:11:56 am    Post subject:

The else statement runs only if the for loop runs to completion. A break statement terminates the loop prematurely and bypasses the else statement. Here's what the tutorial says about the else statement:
Quote:
Loop statements may have an else clause; it is executed when the loop terminates through exhaustion of the list (with for) or when the condition becomes false (with while), but not when the loop is terminated by a break statement.

_________________
Christopher Williams
Back to top
Kllrnohj


/=\ PH34R |\/|3


Joined: 24 May 2005
Posts: 8189

Posted: 22 Aug 2011 01:00:26 am    Post subject:

Frankly you can forget the "for/else" statement exists - it's a little weird and niche.

To figure out what it does, though, just open up an interactive shell and find out. If you just run python without a script argument, it'll let you just start typing commands and interactively running them. By far the best way to figure out python.
_________________
There are only two kinds of programming languages: those people always bitch about and those nobody uses. (Bjarne Stroustrup)
Back to top
MufinMcFlufin


Advanced Member


Joined: 29 Aug 2009
Posts: 252
Location: C:\Users\Admin\Desktop

Posted: 22 Aug 2011 01:07:04 am    Post subject:

Yeah, doing that right now, almost have my first "program" completed by programming functions.
_________________

MufinMcFlufin's quote of the week:
"Diapers and Politicians should both be changed often for the same reason."
-Anonymous
Back to top
KermMartian


Site Admin


Joined: 14 Mar 2005
Posts: 55741
Location: Earth, Sol, Milky Way

Posted: 22 Aug 2011 11:58:49 am    Post subject:

Heartily agreed on using the Python interactive command line. It's a great tool for testing things out without having to make a test script.
_________________


Back to top
elfprince13


OVER NINE THOUSAND!


Joined: 23 May 2005
Posts: 10232
Location: A galaxy far far away......

Posted: 23 Aug 2011 12:18:18 pm    Post subject:

Kllrnohj wrote:
Frankly you can forget the "for/else" statement exists - it's a little weird and niche.

It is fantastic for people who populate their for loops with break statements to make single-entry/exit functions.


Code:

for(;;){ if(condition){ flag = true; break;} }
 if(!flag){ ... }

becomes

Code:

for _ in _:
 if condition: break
else:
 ...




Quote:
To figure out what it does, though, just open up an interactive shell and find out. If you just run python without a script argument, it'll let you just start typing commands and interactively running them. By far the best way to figure out python.

Seconded.
_________________
StickFigure Graphic Productions || VSHI: Vermont Sustainable Heating Initiative


Back to top
Kllrnohj


/=\ PH34R |\/|3


Joined: 24 May 2005
Posts: 8189

Posted: 23 Aug 2011 09:45:10 pm    Post subject:

elfprince13 wrote:
It is fantastic for people who populate their for loops with break statements to make single-entry/exit functions.


I didn't say it wasn't useful, I said he could forget it exists. Syntax sugar should be one of the last things covered when learning a new language, and for/else falls very much into that category. Your example also falls into the "niche" part of what I said Wink
_________________
There are only two kinds of programming languages: those people always bitch about and those nobody uses. (Bjarne Stroustrup)
Back to top
elfprince13


OVER NINE THOUSAND!


Joined: 23 May 2005
Posts: 10232
Location: A galaxy far far away......

Posted: 23 Aug 2011 10:33:48 pm    Post subject:

Kllrnohj wrote:
Your example also falls into the "niche" part of what I said Wink


Where "niche" is the majority of for loops I've had to write in the last 2 years.


And your point about syntactic sugar really depends on your past programming experience - at some point learning new languages is really mostly about learning syntax and the most common bits of the API.
_________________
StickFigure Graphic Productions || VSHI: Vermont Sustainable Heating Initiative


Back to top
christop


Power User


Joined: 09 Mar 2011
Posts: 385
Location: Arizona, USA

Posted: 23 Aug 2011 10:49:36 pm    Post subject:

I find similar control structures pretty useful too, such as for-case and for-while loops. Razz
_________________
Christopher Williams
Back to top
Kllrnohj


/=\ PH34R |\/|3


Joined: 24 May 2005
Posts: 8189

Posted: 25 Aug 2011 10:15:47 pm    Post subject:

elfprince13 wrote:
Where "niche" is the majority of for loops I've had to write in the last 2 years.


Sorry, I'm assuming well designed code here Razz (I kid, I kid)

Quote:
And your point about syntactic sugar really depends on your past programming experience - at some point learning new languages is really mostly about learning syntax and the most common bits of the API.


Absolutely, but you still start with the core syntax, figure out how the API works, then pick up the sugar. Sugar should really always come last unless it is a core part of whatever API you are using (for example, if your working with SQL in C#, absolutely don't skip the LINQ syntax sugar)

And I get the impression he doesn't have much past programming experience here Wink
_________________
There are only two kinds of programming languages: those people always bitch about and those nobody uses. (Bjarne Stroustrup)
Back to top
MufinMcFlufin


Advanced Member


Joined: 29 Aug 2009
Posts: 252
Location: C:\Users\Admin\Desktop

Posted: 31 Aug 2011 10:00:40 pm    Post subject:

Alright, I've been playing around with the console for a while now, and think I'm starting to get the grasp of how to handle Python, however I do have a question I wasn't able to figure out with only the console. I'm wanting to make an IRC Bot, but I don't know what modules to import or where to even start. Any suggestions?
_________________

MufinMcFlufin's quote of the week:
"Diapers and Politicians should both be changed often for the same reason."
-Anonymous
Back to top
Kllrnohj


/=\ PH34R |\/|3


Joined: 24 May 2005
Posts: 8189

Posted: 31 Aug 2011 11:14:05 pm    Post subject:

MufinMcFlufin wrote:
Alright, I've been playing around with the console for a while now, and think I'm starting to get the grasp of how to handle Python, however I do have a question I wasn't able to figure out with only the console. I'm wanting to make an IRC Bot, but I don't know what modules to import or where to even start. Any suggestions?


http://tinyurl.com/qb33ef
_________________
There are only two kinds of programming languages: those people always bitch about and those nobody uses. (Bjarne Stroustrup)
Back to top
KermMartian


Site Admin


Joined: 14 Mar 2005
Posts: 55741
Location: Earth, Sol, Milky Way

Posted: 31 Aug 2011 11:23:53 pm    Post subject:

Jonimus swears by the Python SupyBot package. I myself use irclib for things like SaxJax, and have been quite happy with it.
_________________


Back to top
MufinMcFlufin


Advanced Member


Joined: 29 Aug 2009
Posts: 252
Location: C:\Users\Admin\Desktop

Posted: 01 Sep 2011 12:57:41 am    Post subject:

Kllrnohj wrote:
MufinMcFlufin wrote:
Alright, I've been playing around with the console for a while now, and think I'm starting to get the grasp of how to handle Python, however I do have a question I wasn't able to figure out with only the console. I'm wanting to make an IRC Bot, but I don't know what modules to import or where to even start. Any suggestions?


http://tinyurl.com/qb33ef
Yes, I googled it immediately after I posted the question, I was simply asking for if anyone had any pointers here. :P

No offense, but there's not much a point to a forum if the reply is "go ask google."
_________________

MufinMcFlufin's quote of the week:
"Diapers and Politicians should both be changed often for the same reason."
-Anonymous
Back to top
KermMartian


Site Admin


Joined: 14 Mar 2005
Posts: 55741
Location: Earth, Sol, Milky Way

Posted: 01 Sep 2011 01:01:51 am    Post subject:

Do either of the two packages that I mentioned appeal to you, Mufin? Smile
_________________


Back to top
MufinMcFlufin


Advanced Member


Joined: 29 Aug 2009
Posts: 252
Location: C:\Users\Admin\Desktop

Posted: 01 Sep 2011 02:58:26 am    Post subject:

I remember hearing about irclib from one of my friends who made his own python IRC bots, so I think I'll try that out for a bit. I keep getting errors when trying to connect to irc.esper.net through the console, so might as well try it out.
_________________

MufinMcFlufin's quote of the week:
"Diapers and Politicians should both be changed often for the same reason."
-Anonymous
Back to top
Kllrnohj


/=\ PH34R |\/|3


Joined: 24 May 2005
Posts: 8189

Posted: 01 Sep 2011 11:46:22 pm    Post subject:

MufinMcFlufin wrote:
No offense, but there's not much a point to a forum if the reply is "go ask google."


So don't ask questions that are easily answered by Google, which has the awesome effect of not wasting everyone's time, either. Ask questions Google is unable to answer, and everyone is happy.
_________________
There are only two kinds of programming languages: those people always bitch about and those nobody uses. (Bjarne Stroustrup)
Back to top
elfprince13


OVER NINE THOUSAND!


Joined: 23 May 2005
Posts: 10232
Location: A galaxy far far away......

Posted: 02 Sep 2011 12:32:40 am    Post subject:

Kllrnohj wrote:
MufinMcFlufin wrote:
No offense, but there's not much a point to a forum if the reply is "go ask google."


So don't ask questions that are easily answered by Google, which has the awesome effect of not wasting everyone's time, either. Ask questions Google is unable to answer, and everyone is happy.


How to Ask Smart Questions has some good pointers.
_________________
StickFigure Graphic Productions || VSHI: Vermont Sustainable Heating Initiative


Back to top
KermMartian


Site Admin


Joined: 14 Mar 2005
Posts: 55741
Location: Earth, Sol, Milky Way

Posted: 02 Sep 2011 03:03:47 am    Post subject:

MufinMcFlufin wrote:
I remember hearing about irclib from one of my friends who made his own python IRC bots, so I think I'll try that out for a bit. I keep getting errors when trying to connect to irc.esper.net through the console, so might as well try it out.
I believe you most recently said on #cemetech that you got it to connect, but you hadn't gotten it to speak yet?
_________________


Back to top
MufinMcFlufin


Advanced Member


Joined: 29 Aug 2009
Posts: 252
Location: C:\Users\Admin\Desktop

Posted: 02 Sep 2011 03:10:30 am    Post subject:

KermMartian wrote:
MufinMcFlufin wrote:
I remember hearing about irclib from one of my friends who made his own python IRC bots, so I think I'll try that out for a bit. I keep getting errors when trying to connect to irc.esper.net through the console, so might as well try it out.
I believe you most recently said on #cemetech that you got it to connect, but you hadn't gotten it to speak yet?
I actually literally just finished getting it working, pretty much perfectly.

What I did was added a "zombie mode" so I can chat through it on any channel, and a Minecraft Block/Item ID checking script, which will display the official name of a block and the data value when you ask it diamond (id?).

The zombie mode works perfectly so far, and I'm working on adding a few things to the MC block/item checker. Currently it'll only look at the previous word, which is problematic for blocks like Block of Iron (yes, that's the official name, apparently) so it would only check Iron. So I'm trying to come up with some way for it to check multiple words to be a block, or requiring that you input as one word, or with underscores replacing spaces. I'm also working on adding unofficial names, so Iron Block will report the info of Block of Iron, and the same goes with most all other blocks as well.

Only once both of those are complete, will I attempt to make the spell check I was talking about earlier.
_________________

MufinMcFlufin's quote of the week:
"Diapers and Politicians should both be changed often for the same reason."
-Anonymous
Back to top
Display posts from previous:   
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
    » Goto page 1, 2, 3  Next
» View previous topic :: View next topic  
Page 1 of 3 » All times are GMT - 5 Hours

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

© Copyright 2000-2013 Cemetech & Kerm Martian :: Page Execution Time: 0.045536 seconds.