Ooh, I'm proud of myself. I watched this lecture earlier in the week because I knew my time later in the week would be pretty limited and so I'd started the problem set too. I couldn't understand the second one, though. I was going to ask here and see if someone could help me out, and in phrasing the question, worked out what I'd misunderstood before. :)
elz, you may need to create the poll in another (public) entry. I think polls can only be created at entry posting time, not at edit time.
Hey, I'm all for being a bit of a smartass. I realized I misread the question so I'm going back through all the Python Programming Wikibook examples, which led to this:
question = "Which prime do you wish to find?" print (question) n = raw_input() print ("You wish to find the " + n + "th prime? Fat chance of THAT!"
Man it is times like these when I know how non-mathy my brain is. I got all the primes between 1 and 1000 but my code is ungainly and ugly and I kind of just printed 2, 3, 5, and 7 before I got the ball rolling on the rest by nesting 'if' and n%2!=0, n%3!=0 ... etc.
Anybody else make a pretty ugly program? I'm gonna go try it again at some point tonight. How'd your approach differ from mine?
Trying to break it down can help a lot: how would you check to see if a particular number (something small, maybe 5 or 6) was prime or not? If you write out the steps, just in plain English, that can be a roadmap for what you have to translate into code. Once you know how to check to see if a particular number is prime, you'd want to figure out how to generalize the steps so you can check to see if any given number is prime. To figure out the nth prime number, you just need a way to keep track of the ones you already know and count the length of the list.
And this:
You might think about which integers you need to check as divisors – certainly you don’t need to go beyond the candidate you are checking, but how much sooner can you stop checking?
is a hint that saves a lot of work. If you want to know if 5 is prime, do you need to divide it by 4? If not, why not?
(And I'd say definitely worry about one at a time!)
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.
I typed up the examples he used while teaching since we don't have the handouts. This might help to get a better understanding of what he was doing. Everything is commented out also, so don't forget to remove the pound sign before playing. ^_^ Hopefully I typed it all correctly, I haven't played around with it yet.
ETA or you can just go to the lecture video at the MIT website and they have the handout right there.
Examples of overloading 3*4 3*'ab' 'a' + 'bcd' 3 + 'ab' str(3)+'ab' 'a'<3 4<'3' '4'<'3' The code he used
##x=3 #create variable x and assign value 3 to it ##x=x*x # bind x to value 9 ##print x ##n = raw_input('Enter a number; ') ##print n ##print n/n
##x=15 ##y=5 ##z=11 ##print x,y,z ###Is this right? ##if x< y: ## if x< z : print 'x is least' ## else: print 'z is least' ##else: print 'y is least' ## ##if x < y and x < z : print 'x is least' ##elif y < z : print 'y is least' ##else: print 'z is least'
no subject
Date: 2009-11-11 10:42 pm (UTC)no subject
Date: 2009-11-11 10:48 pm (UTC)NASnMmOJgL
Date: 2012-01-06 10:20 pm (UTC)no subject
Date: 2009-11-12 12:06 am (UTC)[x]
no subject
Date: 2009-11-12 12:13 am (UTC)no subject
Date: 2009-11-12 04:30 am (UTC)no subject
Date: 2009-11-12 05:06 am (UTC)question = "Which prime do you wish to find?"
print (question)
n = raw_input()
print ("You wish to find the " + n + "th prime? Fat chance of THAT!"
aqgHeNVyWCUz
Date: 2012-01-07 05:23 am (UTC)no subject
Date: 2009-11-12 12:13 am (UTC)Anybody else make a pretty ugly program? I'm gonna go try it again at some point tonight. How'd your approach differ from mine?
no subject
Date: 2009-11-12 01:30 am (UTC)no subject
Date: 2009-11-12 05:55 am (UTC)(I realized I totally got off-track and tried to get an answer the problem didn't ask for.)
pXWMiprUPTOVcxLS
Date: 2012-01-07 09:28 am (UTC)MoDeXpCzNyFKFCSvSS
Date: 2011-08-14 05:14 am (UTC)vqiwThxRBr
Date: 2012-01-09 05:19 am (UTC)no subject
Date: 2009-11-12 12:16 am (UTC)no subject
Date: 2009-11-12 01:31 am (UTC)And this:
You might think about which integers you need to check as divisors – certainly you don’t need to go beyond the candidate you are checking, but how much sooner can you stop checking?
is a hint that saves a lot of work. If you want to know if 5 is prime, do you need to divide it by 4? If not, why not?
(And I'd say definitely worry about one at a time!)
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)no subject
Date: 2009-11-12 12:35 am (UTC)no subject
Date: 2009-11-12 01:06 am (UTC)no subject
Date: 2009-11-12 02:00 am (UTC)no subject
Date: 2009-11-12 02:11 am (UTC)Examples used in Lecture
Date: 2009-11-14 12:06 pm (UTC)ETA or you can just go to the lecture video at the MIT website and they have the handout right there.
Examples of overloading
3*4
3*'ab'
'a' + 'bcd'
3 + 'ab'
str(3)+'ab'
'a'<3
4<'3'
'4'<'3'
The code he used
##x=3 #create variable x and assign value 3 to it
##x=x*x # bind x to value 9
##print x
##n = raw_input('Enter a number; ')
##print n
##print n/n
##x=15
##if (x/2)*2 == x:
## print 'Even'
##else: print 'Odd'
##x=15
##if (x/2)*2 == x: print 'Even'
##else: print 'Odd'
##z='b'
##if 'x' < z :
## print 'Hello'
## print 'Mom'
##if 'x'< z :
## print 'Hello'
##print 'Mom'
##x=15
##y=5
##z=11
##print x,y,z
###Is this right?
##if x< y:
## if x< z : print 'x is least'
## else: print 'z is least'
##else: print 'y is least'
##
##if x < y and x < z : print 'x is least'
##elif y < z : print 'y is least'
##else: print 'z is least'
##y=0
##x=3
##itersLeft = x
##while (itersLeft>0):
## y= y+x
## itersLeft = itersLeft -1
###print 'y =',y,',intersLeft=',intersLeft
##print y
##x=10
##i=1
##while(i<x): ## if x%i == 0: ## print 'divisor ',i ## i = i+1 ##x = 10 ##for i in range(1,x): ## if x%i == 0: ## print 'divisor ',i
hqFbGesiTVFUz
Date: 2012-01-09 03:48 am (UTC)