That's good, though! Understanding the problem is a huge part of it. So, generally, when you use a loop, you want a number that changes each time it runs and an end condition to test. Do you know what those would be and what variables you would need to keep track of them?
I think my problem is that I generally do get the problem itself (even though prime numbers were something I never really understood, for some reason), but I have absolutely no idea how to write code. I can understand it, the examples all make sense to me, but I can't really produce any myself. It's as if I had just learned to say "hello" and "how are you" in a foreign language, and then someone suddenly asks me for a detailed description of my day. I know what I want to say, obviously, but I have no idea how to say it.
I think this is a problem that I'll be having with this, if I choose to continue. The lecture and the reading material don't seem to be particularly helpful either.
For what it's worth, when I was first learning to code, I Really Didn't Get It for a while, and I needed to have my friends sit by me and hold my hand and walk me through examples like I was the dumbest person to ever walk the earth.
After a while, though, I did hit critical mass, and the learning curve flattened out a *lot.* I know that "keep on trying, you'll get it" isn't always the most useful thing to say in situations like this, but....
It might be useful for you to, for a while, explicitly write out the pseudocode for anything you want to code. That way, you're forcing yourself to think algorithmically, but you're writing English steps instead of Python ones, so it's a little more familiar.
For example, if you're checking to see that a number is prime, then the pseudocode might look like:
n = the number we're checking start with x = 2, 'cause it's the smallest prime while x < n if n is divisible by x n is not prime, we can stop checking now otherwise, n might still be prime! set x to the next value to check divisibility with, and run through the loop again
My problem isn't that I can't think how the code is supposed to work. I can do that, so your example isn't really helping me any. My problem is that I have no clue how to put you example into actual working code. This might be the most obvious thing ever for you, but for me, it's the thing that's standing in my way. I've run into this problem before with HTML, so it isn't new. With HTML, I managed to work my way through it by tinkering with existing and working code to figure out how to adapt it to my purposes (I'm still starting with a collage of copypasta rather than actual writing when I'm not comfortable with an element). I haven't found a similar way to deal with PHP, and I'm not sure if there is such a thing for Python since it seems more specific to me.
I don't know if I'm helping or being annoying but I went back and worked through each example in the Python Programming Wikibook and tinkered with each one, seeing what variations did, so I still have no clue how to implement ungemmed's pseudocode yet (that's about as far as I got with my own) but at least I'm getting the idea of how to define variables and manipulate them.
I'm a lot like you, I think. This is my first non-HTML code because I self-taught in HTML by taking other people's source code and manipulating it to see how they'd done what they did. Hang in there a little longer and see if it clicks? If not, I don't mean to push you or frustrate you more.
I'm glad to read these comments--I haven't had a chance to start working on this problem set for real yet, but I had to talk myself down from panicking. I'm going to try your suggestion and work through the rest of the examples, and perhaps some of it will start to sink in.
I'm grateful to have a community working on this stuff--thanks to all of you who have commented.
Hmm, okay. I sometimes run into the same problem, I think every coder does--I mean, obviously you're running into it more than most, but I've definitely had that "argh, why do computers not speak English" feeling before.
If it helps, here's how I'd translate the pseudocode I just gave you into real python:
n = the number we're checking
n = int(raw_input('n? '))
(raw_input prompts the user and returns their answer; we need to typecast the input from a string to a number using int, or otherwise the computer will think it's text and whine at us) start with x = 2, 'cause it's the smallest prime
x = 2
(nothing really fancy there) while x < n
while x < n:
(another place where the python bears a striking resemblance to the pseudocode)
if n is divisible by x
if x % n == 0:
(use the modulus function to see if x divides evenly into n)
n is not prime, we can stop checking now
break
(breaks you out of the while loop, and sets your "instruction counter" at the instruction immediately after it)
otherwise,
else
*
n might still be prime! (since our program is assuming n is prime until told otherwise, already, there's no need to translate this into code)
set x to the next value to check divisibility with
x += 1
(sets x to old-x + 1)**
and run through the loop again (you don't actually need to put anything here for this, in python. When the interpreter reaches the end of a while loop, it will automatically go back to the top, assuming the loop condition (x < n) still holds true)
*You don't actually need to use an else case for this, because there's a break statement in the if case, but it's a more direct translation of the pseudocode. **It would be faster to only check n's divisibility with just primes, rather than with all integers, but that is beyond the scope of this simple example. ^^
I need a lightbulb icon! I just tried to write a comment telling you how I got it but I rambled but I think I've at least got how I was thinking in the wrong direction, basically. I couldn't even start. Thank you!
Though your pseudocode and translation aren't particularly helpful to me at the moment, reading about your past need for handholding does. I have a feeling I'm going to need to sit with a programming pal this evening and try to work it out, but it's comforting to know that that may not mean I'm doomed to never be capable on my own. So, thank you!
It's as if I had just learned to say "hello" and "how are you" in a foreign language, and then someone suddenly asks me for a detailed description of my day. I know what I want to say, obviously, but I have no idea how to say it.
If it makes you feel any better, I've been drowning in that feeling while working my way through the *very first* exercise (I'm running late - lecture 2 is for tomorrow for me) and thank you for pinning it down so exactly!
I was feeling like most of the reading material was also written in that same foreign language, but I did get there in the end.
no subject
Date: 2009-11-12 01:49 am (UTC)no subject
Date: 2009-11-12 02:02 am (UTC)no subject
Date: 2009-11-12 02:14 am (UTC)I think this is a problem that I'll be having with this, if I choose to continue. The lecture and the reading material don't seem to be particularly helpful either.
no subject
Date: 2009-11-12 05:30 am (UTC)After a while, though, I did hit critical mass, and the learning curve flattened out a *lot.* I know that "keep on trying, you'll get it" isn't always the most useful thing to say in situations like this, but....
It might be useful for you to, for a while, explicitly write out the pseudocode for anything you want to code. That way, you're forcing yourself to think algorithmically, but you're writing English steps instead of Python ones, so it's a little more familiar.
For example, if you're checking to see that a number is prime, then the pseudocode might look like:
n = the number we're checking
start with x = 2, 'cause it's the smallest prime
while x < n
if n is divisible by x
n is not prime, we can stop checking now
otherwise,
n might still be prime!
set x to the next value to check divisibility with, and run through the loop again
no subject
Date: 2009-11-12 05:51 am (UTC)no subject
Date: 2009-11-12 06:03 am (UTC)I'm a lot like you, I think. This is my first non-HTML code because I self-taught in HTML by taking other people's source code and manipulating it to see how they'd done what they did. Hang in there a little longer and see if it clicks? If not, I don't mean to push you or frustrate you more.
no subject
Date: 2009-11-13 09:33 pm (UTC)I'm grateful to have a community working on this stuff--thanks to all of you who have commented.
no subject
Date: 2009-11-14 02:40 am (UTC)yvixSJkxBtcbHfBLqiT
Date: 2012-01-09 04:22 am (UTC)no subject
Date: 2009-11-12 06:21 am (UTC)If it helps, here's how I'd translate the pseudocode I just gave you into real python:
n = the number we're checking
(raw_input prompts the user and returns their answer; we need to typecast the input from a string to a number using int, or otherwise the computer will think it's text and whine at us)
start with x = 2, 'cause it's the smallest prime
(nothing really fancy there)
while x < n
(another place where the python bears a striking resemblance to the pseudocode)
if n is divisible by x
(use the modulus function to see if x divides evenly into n)
n is not prime, we can stop checking now
(breaks you out of the while loop, and sets your "instruction counter" at the instruction immediately after it)
otherwise,
*
n might still be prime!
(since our program is assuming n is prime until told otherwise, already, there's no need to translate this into code)
set x to the next value to check divisibility with
(sets x to old-x + 1)**
and run through the loop again
(you don't actually need to put anything here for this, in python. When the interpreter reaches the end of a while loop, it will automatically go back to the top, assuming the loop condition (x < n) still holds true)
*You don't actually need to use an else case for this, because there's a break statement in the if case, but it's a more direct translation of the pseudocode.
**It would be faster to only check n's divisibility with just primes, rather than with all integers, but that is beyond the scope of this simple example. ^^
no subject
Date: 2009-11-16 05:15 am (UTC)no subject
Date: 2009-11-16 05:18 am (UTC)LjdywZEBondMs
Date: 2011-08-14 05:08 am (UTC)no subject
Date: 2009-11-12 09:29 am (UTC)JQGKyQUiLhGaNrGPR
Date: 2011-08-14 06:59 am (UTC)jBLQAbcTXgxeuAast
Date: 2012-01-07 08:24 am (UTC)no subject
Date: 2009-11-14 08:34 pm (UTC)If it makes you feel any better, I've been drowning in that feeling while working my way through the *very first* exercise (I'm running late - lecture 2 is for tomorrow for me) and thank you for pinning it down so exactly!
I was feeling like most of the reading material was also written in that same foreign language, but I did get there in the end.
I'm hoping it will all get easier with practise!
BbJLPbCRReuw
Date: 2012-01-06 10:05 pm (UTC)