I'm doing Problem 1.1 here: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-00-introduction-to-computer-science-and-programming-fall-2008/assignments/pset1a.pdf I'm finding all odd numbers first. How can I turn this into a function called findodd? ''' c = 0 a = 1 x = int(raw_input("Enter your limit. ")) for a in range(1, x): while a % 2 > 0: print a, "is odd" a = a + 1 c = (c + 1) break else: print a, "is even" a = a + 1 c = c print "There are", c, "total odd numbers" '''
This is not the final procedure to find odds for the Problem Set question, by the way. I am thinking my next step would be to take out the "prints" and if the number is odd, add it to a candidate list that I can use later on in the process of determining higher primes. Am I on the right track there at all, either?
``` >>> >>> def foo(n): n = n + 2 n = n / 3. return n >>> foo(5) 2.3333333333333335 >>> ``` http://docs.python.org/2.7/tutorial/controlflow.html#defining-functions
This function tell you if the number(x) is odd or even. def odd_or_even(x): if x%2 == 0: print x, "is even" else: print x, "is odd"
Thanks for the help, both of you. I was just overthinking it, under-testing it, rushing, and putting everything in the wrong place. lol :P I think I've got it figured out now. I'm going to post the finished assignment in a separate question for feedback. Thanks again!
You're welcome.
Join our real-time social learning platform and learn together with your friends!