Quick (probably noob) question...

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

Quick (probably noob) question...

Postby hyper » Mon Aug 19, 2013 7:59 pm

Hi there!
Just started out learning python and have hit a boulder....
I've been following learnphython.org's tutorials (if you know a better website please suggest) but the exercise on loops has got me stumped this is the question and what I have made so far...

Q. Loop through and print out all even numbers from the numbers list in the same order they are received. Don't print any numbers that come after 237 in the sequence.
(the sequence of numbers was already given)

This is what I have:
Code: Select all
numbers = [
    951, 402, 984, 651, 360, 69, 408, 319, 601, 485, 980, 507, 725, 547, 544,
    615, 83, 165, 141, 501, 263, 617, 865, 575, 219, 390, 984, 592, 236, 105, 942, 941,
    386, 462, 47, 418, 907, 344, 236, 375, 823, 566, 597, 978, 328, 615, 953, 345,
    399, 162, 758, 219, 918, 237, 412, 566, 826, 248, 866, 950, 626, 949, 687, 217,
    815, 67, 104, 58, 512, 24, 892, 894, 767, 553, 81, 379, 843, 831, 445, 742, 717,
    958, 609, 842, 451, 688, 753, 854, 685, 93, 857, 440, 380, 126, 721, 328, 753, 470,
    743, 527
]
y=0
x = numbers[y]
print x

while True:
    if x == 237:
      break
    if x % 2 ==1:
      continue
    print x
    y += 1
    x = numbers[y]


so yeah, any help would be really appreciated..
hyper
 
Posts: 4
Joined: Mon Aug 19, 2013 7:52 pm

Re: Quick (probably noob) question...

Postby micseydel » Mon Aug 19, 2013 8:24 pm

What attempt have you made? We'd like to see the code you've written to attempt this, even if it's wrong, so that we have some idea of how to help you, because right now for us to do anything more than recommend a basic tutorial would be work redundant to that which the tutorial writers have done.
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: 1497
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Quick (probably noob) question...

Postby hyper » Mon Aug 19, 2013 9:14 pm

Sorry I didn't make it that clear, the code i have written is this:
Code: Select all
y=0
x = numbers[y]
print x

while True:
    if x == 237:
      break
    if x % 2 ==1:
      continue
    print x
    y += 1
    x = numbers[y]


it's just the beginning bit (the Numbers list) you are given to start with.
hyper
 
Posts: 4
Joined: Mon Aug 19, 2013 7:52 pm

Re: Quick (probably noob) question...

Postby hyper » Mon Aug 19, 2013 9:55 pm

just an update I've edited it a bit to make it look a bit nicer, here it is:

Code: Select all
numbers = [
    951, 402, 984, 651, 360, 69, 408, 319, 601, 485, 980, 507, 725, 547, 544,
    615, 83, 165, 141, 501, 263, 617, 865, 575, 219, 390, 984, 592, 236, 105, 942, 941,
    386, 462, 47, 418, 907, 344, 236, 375, 823, 566, 597, 978, 328, 615, 953, 345,
    399, 162, 758, 219, 918, 237, 412, 566, 826, 248, 866, 950, 626, 949, 687, 217,
    815, 67, 104, 58, 512, 24, 892, 894, 767, 553, 81, 379, 843, 831, 445, 742, 717,
    958, 609, 842, 451, 688, 753, 854, 685, 93, 857, 440, 380, 126, 721, 328, 753, 470,
    743, 527
]

count = 0
while numbers[count] == 237:
    if numbers[count] % 2 == 1:
      continue
    else:
        print numbers[count]
        count += 1
hyper
 
Posts: 4
Joined: Mon Aug 19, 2013 7:52 pm

Re: Quick (probably noob) question...

Postby micseydel » Mon Aug 19, 2013 9:56 pm

What about your code doesn't do what you want? What does it do, versus what you are attempting to achieve?
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: 1497
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Quick (probably noob) question...

Postby hyper » Mon Aug 19, 2013 10:01 pm

according to the onsite code interpreter thing, the code does nothing and I want it to Loop through and print out all even numbers from the numbers list in the same order they are received. Don't print any numbers that come after 237 in the sequence.
hyper
 
Posts: 4
Joined: Mon Aug 19, 2013 7:52 pm

Re: Quick (probably noob) question...

Postby micseydel » Mon Aug 19, 2013 10:06 pm

Both sets of code do very different things, the first one is much closer. I'm not sure why the first one apparently "does nothing" but before we bother with what the thing you're using says, run it locally and figure out for yourself whether it's working properly as not, then once you believe it is correct submit it to that website.

Your second bit of code basically says "while the current number I'm iterating through is equal to 237, do (...thing)". But your first number isn't 237, so it immediately exits. You need to rethink the logically.

That said, for loops are generally very much preferable to while loops. Just do "for number in numbers:" then apply your logic inside of the loop to each number. Print when you want to, and break when you need to. You don't want to manually keep track of your own index value (you use the variable y here for that).
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: 1497
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA


Return to General Coding Help

Who is online

Users browsing this forum: wichita12 and 5 guests