Author Topic: Coding Help  (Read 4168 times)

0 Members and 1 Guest are viewing this topic.

Offline Sk33ter

  • /dev/null
  • *
  • Posts: 9
  • Cookies: -4
    • View Profile
Coding Help
« on: April 01, 2012, 06:48:29 pm »
I'm learning python and I'm on for and while loops. As i don't fully get for loops, I also can not figure out the answer to the exercise. The exercise is:

Loop through and print out all even numbers from the numbers list in the same order they are received, but only up to the number "412" (not including it) Stop there and ignore all numbers past 412 in the sequence. (Note there will be numbers over 412.)
Code: [Select]
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
]

# your code goes here




and that is the code they gave me so far.

So in short could someone clarify for loops and help me through the exercise (please don't just tell me the answer).

Thanks!

Offline Kulverstukas

  • Administrator
  • Zeus
  • *
  • Posts: 6627
  • Cookies: 542
  • Fascist dictator
    • View Profile
    • My blog
Re: Coding Help
« Reply #1 on: April 01, 2012, 07:10:50 pm »
well I was about to write the code for ya, because there is really nothing to explain :P loops are same everywhere and python is no exception.
I don't know how to explain it without giving an example which is really a solution to your problem :D
But here is the basic loop you have to use, to fit your problem you will have to figure how to stop at a certain number:

Code: [Select]
i = 0
for i in numbers:
    # your checking code goes here
    print i
You must input your checking code instead of the comment, remember that number 412  should not be printed but execution must be terminated  on it. So "if equal to" should be your solution ;)

Sorry if I gave out too much, I'm a bad teacher :D
« Last Edit: April 01, 2012, 07:11:09 pm by Kulverstukas »

Offline techb

  • Soy Sauce Feeler
  • Global Moderator
  • King
  • *
  • Posts: 2350
  • Cookies: 345
  • Aliens do in fact wear hats.
    • View Profile
    • github
Re: Coding Help
« Reply #2 on: April 01, 2012, 10:47:28 pm »
here is an example of a for loop.


Code: [Select]


my_list = [1,2,3,4,5,6,7,8,9,10]


for number in my_list:
    if number > 5:
        print number




Note that this is the simplest form of a for loop. You can iterate over multiple items by using the zip() method.


You can also embed for loops in the list itself called list comprehension.


Example of list comprehension:
Code: [Select]
new_list = [ i for i in range(0,10) ]


Now to check if a number is even or not, use the % operator to check for remainders.
Example:
Code: [Select]
>>7%2
1
>>
>>8%2
0


You can see checking for remainders with 2 will tell you if its even or not. No remainders == even.
>>>import this
-----------------------------

Offline s3my0n

  • Knight
  • **
  • Posts: 276
  • Cookies: 58
    • View Profile
    • ::1
Re: Coding Help
« Reply #3 on: April 02, 2012, 01:35:40 am »
Code: [Select]
for i in numbers:
    if i is equal to 412 break from the loop
    if i is even print i
Easter egg in all *nix systems: E(){ E|E& };E

Offline Sk33ter

  • /dev/null
  • *
  • Posts: 9
  • Cookies: -4
    • View Profile
Re: Coding Help
« Reply #4 on: April 02, 2012, 04:17:00 am »
OK I've tried everything but it keeps saying:

Traceback (most recent call last):
  File "/base/data/home/apps/s~learnpythonjail/1.354953192642593048/main.py", line 99, in post
    exec(cmd, safe_globals)
  File "<string>", line 14
     if equal to 412
               ^
 SyntaxError: invalid syntax

Code: [Select]
i = 0
for i in numbers:
    if equal to 412 break
    print i

also tried:

Code: [Select]
for i in numbers:
    if i == 412 break
    print numbers



Offline techb

  • Soy Sauce Feeler
  • Global Moderator
  • King
  • *
  • Posts: 2350
  • Cookies: 345
  • Aliens do in fact wear hats.
    • View Profile
    • github
Re: Coding Help
« Reply #5 on: April 02, 2012, 05:49:27 am »
We're not going to give you a direct answer. Between all the answers given you should know the answer.

We're not going to give you homework answers, you'll need to learn the answer. No silver spoons here.
>>>import this
-----------------------------

Offline lucid

  • #Underground
  • Titan
  • **
  • Posts: 2683
  • Cookies: 243
  • psychonaut
    • View Profile
Re: Coding Help
« Reply #6 on: April 02, 2012, 06:34:47 am »
Syntax errors are some of the most frustrating errors because often times it's one small char out of place....

Try squinting
"Hacking is at least as much about ideas as about computers and technology. We use our skills to open doors that should never have been shut. We open these doors not only for our own benefit but for the benefit of others, too." - Brian the Hacker

Quote
15:04  @Phage : I'm bored of Python

Offline Kulverstukas

  • Administrator
  • Zeus
  • *
  • Posts: 6627
  • Cookies: 542
  • Fascist dictator
    • View Profile
    • My blog
Re: Coding Help
« Reply #7 on: April 02, 2012, 10:56:50 am »
Well s3my0n pretty much gave you the answer. You just have to replace words with symbols.

xor

  • Guest
Re: Coding Help
« Reply #8 on: April 02, 2012, 01:30:10 pm »
Tip 1: A for loop is to be used when you know in advance, or can easily discover the amount of elements that are going to be iterated over.

Example:

Code: [Select]
for (int start=0; start <= amountToLoopThrough; start++)
{
    //  ^^ starting value     ^^ known value      ^^ post iteration (usually an increment)
    // do something with element.
}


Tip 2: A while loop is used when you are unsure when you will need to break out of the processing loop.

Example:

Code: [Select]
while (program_isnt_closing)
{
    //  ^^ Don't know when the program is going to close.
    doSomething();
}



Tip 3: Modulo (mathematical) can be used to calculate remainders after division.


All programming languages have the modulo operator built in. This is very useful when determining whether a value is a factor of another value. Exceptionally useful for determining whether a number is odd or even.


Example:


Code: [Select]
int number = 20;


if ((number mod 2) == 0)
{
   // if a number is divisible by 2 and has 0 remainder. It's an even number.
   // Some languages it's keyword is MOD in others it's the % symbol.
    printf("Number is even!");

}
« Last Edit: April 02, 2012, 01:31:22 pm by xor »

Offline flowjob

  • Knight
  • **
  • Posts: 327
  • Cookies: 46
  • Pastafarian
    • View Profile
Re: Coding Help
« Reply #9 on: April 02, 2012, 04:11:53 pm »
@Sk33ter
The code you got from us is pseudo-code and won't work in Python,'cause Python has

    1. A other Syntax
    2. Other commands and operators (e.g. as you already discovered 'equal to' won't work in
                                                      Python but '==' will)

And as techb said,you won't get the finished code from us,so you'll have to change the pseudo-code to Python-code.

And your last snippet is almost correct. Tip: look at the error-message,it tells you what's wrong at your synatx (the little '^' shows you where the wrong syntax is/starts)
« Last Edit: April 02, 2012, 04:18:20 pm by Area_13 »
Quote
<phil> I'm gonna DDOS the washing machine with clothes packets.
<deviant_sheep> dont use too much soap or youll cause a bubble overflow

Offline Python

  • /dev/null
  • *
  • Posts: 6
  • Cookies: 0
    • View Profile
Re: Coding Help
« Reply #10 on: April 02, 2012, 04:47:50 pm »
Code: [Select]
i = 0
for i in numbers:
    if equal to 412 break
    print i

also tried:

Code: [Select]
for i in numbers:
    if i == 412 break
    print numbers


Ok, the first example above is pseudo code. By looking at the second example above, it would appear that you have realized this. Though you don't seem to be applying what you have previously learned to this example. Every other time you have written 'if x == y' what have you done? It isn't what you done in that attempt. Have a look at the example below and see what is different.
Code: [Select]
i = 99
if i == 99:
    print 'The value is 99'
Hint, look at the indentation


I suggest you go over what you have already covered, you don't seem to have a good grasp of basic python syntax yet.
« Last Edit: April 02, 2012, 04:49:45 pm by Python »

Offline Sk33ter

  • /dev/null
  • *
  • Posts: 9
  • Cookies: -4
    • View Profile
Re: Coding Help
« Reply #11 on: April 02, 2012, 09:22:00 pm »
Ok so I got it, but it still prints out 412.

Code: [Select]
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
]

# your code goes here
for i in numbers:
    print i
    if i == 412:
        break

I need it so it does not print 412

Loop through and print out all even numbers from the numbers list in the same order they are received, but only up to the number "412" (not including it) Stop there and ignore all numbers past 412 in the sequence. (Note there will be numbers over 412.)

Offline Kulverstukas

  • Administrator
  • Zeus
  • *
  • Posts: 6627
  • Cookies: 542
  • Fascist dictator
    • View Profile
    • My blog
Re: Coding Help
« Reply #12 on: April 02, 2012, 09:25:57 pm »
do printing after checking

Offline Sk33ter

  • /dev/null
  • *
  • Posts: 9
  • Cookies: -4
    • View Profile
Re: Coding Help
« Reply #13 on: April 03, 2012, 02:10:04 am »
Thank you all for the help !  :D

Code: [Select]
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
]

# your code goes here
for i in numbers:
    if i == 412:
        break
    print i

xor

  • Guest
Re: Coding Help
« Reply #14 on: April 03, 2012, 01:41:35 pm »
Make sure that you're not misinterpreting the requirements.

You said that once you get to the element 412 that you have to stop. Are you sure they didn't mean that you should print out all numbers that are less than but not equal to 412?

If this is the case, then as soon as your program hits 412 it will stop, but there are lots of even numbers smaller than 412 after that one in the list.

ex. 248, 104, 58, 24, 380, 126, 328

This seems like the more likely scenario that they would ask for. However, to cover your ass (if this is an assignment), if there is any doubt as to what they're really asking for, ask them again, or write both ways of doing it and submit that.