Break statements and other issues

This is the place for queries that don't fit in any of the other categories.

Break statements and other issues

Postby Hyphy » Tue Oct 08, 2013 8:15 pm

I am currently learning Python out of a book, gearing up for classes starting in January. I wanted to get a jump start on some programming just to get my feet wet, and to see how things work. I am using the book called "Python Programming for the absolute beginner" 3rd edition. I downloaded python 3.1 from their companion site so I would be sure to have to version they used in the book.

Since starting, I have noticed a few quirks. For example, I wrote a program from the book that was to display the words "Game Over" written in slashes. The program worked fine for the "Game" part, but broke up the word "OVER" for some reason. After fiddling with it, I realized that the last bit of the "R" ended in \. Apparently the program didn't like it, so it messed up the whole word. I solved this by making an exclamation point at the end of the word "over", so the end of the row was | instead of \. NO big deal really, I mean how many times do you end up programming something with a slash at the end?

Now, a more annoying quirk has popped up. Apparently, there is some strange syntax problem with break statements in loops. I copied a code out of the book that was supposed to count from 0 to 10 and if count>10, the program would break and give users the option to leave the program. Well, IDLE keeps yelling at me that break is "outside of the loop". I tried random things to see if I could get this or that little change to work, shifting the line, putting the command inside parenthesis, all sorts of junk that I figured probably wouldn't work.

My husband thinks it has something to do with the AMD processor on my machine. It is a laptop with WIN8, but this also happens on my Win7 desktop. All of our machines have AMDs of various generations. Does anyone know what is up with these weird quirks? Did I snag a different version of python or is my processor messing with this stuff? I want to figure this out, cuz it is driving me nuts!
Last edited by Mekire on Wed Oct 09, 2013 12:55 am, edited 1 time in total.
Reason: First post lock
Hyphy
 
Posts: 4
Joined: Tue Oct 08, 2013 7:59 pm

Re: Break statements and other issues

Postby stranac » Tue Oct 08, 2013 8:43 pm

No, this has nothing to do with the processor you have.

We can't really tell what's going on without seeing your code, so you should post it.
Also post the errors you get, if any.

Make sure you've read this before posting.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1245
Joined: Thu Feb 07, 2013 3:42 pm

Re: Break statements and other issues

Postby micseydel » Tue Oct 08, 2013 10:40 pm

Just to reiterate, your processor should never impact your Python code, with the exception being a faulty processor. As for the operating system, most Python code is entirely unaffected by it, especially when you code correctly, like using the os module instead of trying to manipulate paths yourself (since Windows differs from Unix-like systems such as Mac OS X and Linux). There are small exception for performance as well, but don't worry about that. Also, before long, you'll intuitively know when certain things are operating system dependent and when they're not.

Also: if you're asking or commenting about code, always always always post that code.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1494
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Break statements and other issues

Postby Hyphy » Wed Oct 09, 2013 5:22 pm

Sorry about that! I remebered that I needed to post the code right after I sent the post in for approval. SO I had to wait!

Code: Select all
count=0
while True:
    count+=1
#end loop if count greater than 10
if count>10:
    break
#skip 5
if count == 5:
    continue
print(count)

input("\nPress ENTER to exit")


That is the code I copied directly out of the book. When I run it in shell, it highlights the portion after the 'break' command then sends up a Syntax error saying that 'break' is outside of loop.

Sorry for forgetting to post the code! >.<
Hyphy
 
Posts: 4
Joined: Tue Oct 08, 2013 7:59 pm

Re: Break statements and other issues

Postby Hyphy » Wed Oct 09, 2013 5:30 pm

Also, I apologize for not posting the Traceback. I am a total noob and I can't get it to pop up.
Hyphy
 
Posts: 4
Joined: Tue Oct 08, 2013 7:59 pm

Re: Break statements and other issues

Postby stranac » Wed Oct 09, 2013 5:36 pm

Yes, this is obviously broken code.
My guess would be you wanted something like:
Code: Select all
count=0
while True:
    count+=1
    #end loop if count greater than 10
    if count>10:
        break
    #skip 5
    if count == 5:
        continue
    print(count)

input("\nPress ENTER to exit")
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1245
Joined: Thu Feb 07, 2013 3:42 pm

Re: Break statements and other issues

Postby Hyphy » Wed Oct 09, 2013 5:41 pm

Well that's what I get for trusting in a book, where non-computer editors might move things. I had a bit of "The book must be right!" tunnel vision. Thanks for helping me! I feel a bit silly! :)
Hyphy
 
Posts: 4
Joined: Tue Oct 08, 2013 7:59 pm

Re: Break statements and other issues

Postby micseydel » Wed Oct 09, 2013 5:49 pm

Hyphy wrote:Well that's what I get for trusting in a book, where non-computer editors might move things. I had a bit of "The book must be right!" tunnel vision. Thanks for helping me! I feel a bit silly! :)

Well when I look at pages 72-73 at http://www.amazon.com/Python-Programmin ... 1435455002 it looks like they have it right. Perhaps it's harder to tell when those pages are side by side instead of next to each other at top and bottom. If you want to be a programmer though, you're going to have to read something like "break outside of loop" then look at the code and see what you need to change; if you understand the role of indentation, you'd see that in your original code the break is not in the loop but could easily moved in.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1494
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA


Return to General Coding Help

Who is online

Users browsing this forum: Google [Bot] and 2 guests