EvilZone

Programming and Scripting => Scripting Languages => : Sk33ter April 01, 2012, 06:48:29 PM

: Coding Help
: Sk33ter 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.)
:
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!
: Re: Coding Help
: Kulverstukas 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:

:
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
: Re: Coding Help
: techb April 01, 2012, 10:47:28 PM
here is an example of a for loop.


:


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:
:
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:
:
>>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.
: Re: Coding Help
: s3my0n April 02, 2012, 01:35:40 AM
:
for i in numbers:
    if i is equal to 412 break from the loop
    if i is even print i
: Re: Coding Help
: Sk33ter 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

:
i = 0
for i in numbers:
    if equal to 412 break
    print i

also tried:

:
for i in numbers:
    if i == 412 break
    print numbers


: Re: Coding Help
: techb 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.
: Re: Coding Help
: lucid 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
: Re: Coding Help
: Kulverstukas April 02, 2012, 10:56:50 AM
Well s3my0n pretty much gave you the answer. You just have to replace words with symbols.
: Re: Coding Help
: xor 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:

:
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:

:
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:


:
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!");

}
: Re: Coding Help
: flowjob 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)
: Re: Coding Help
: Python April 02, 2012, 04:47:50 PM
:
i = 0
for i in numbers:
    if equal to 412 break
    print i

also tried:

:
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.
:
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.
: Re: Coding Help
: Sk33ter April 02, 2012, 09:22:00 PM
Ok so I got it, but it still prints out 412.

:
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.)
: Re: Coding Help
: Kulverstukas April 02, 2012, 09:25:57 PM
do printing after checking
: Re: Coding Help
: Sk33ter April 03, 2012, 02:10:04 AM
Thank you all for the help !  :D

:
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
: Re: Coding Help
: xor 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.
: Re: Coding Help
: Kulverstukas April 03, 2012, 02:00:22 PM
actually you should use IF/ELSE, because now the code after the break is what people call "unreachable code".
http://en.wikipedia.org/wiki/Unreachable_code
It's not the true definition but you get the point :P

For small script like your this solution works, but for bigger programs this may cause really really bad headaches when debugging. And I know the feel :D

EDIT: xor is right, your requirements are not very clear.
: Re: Coding Help
: xor April 03, 2012, 02:16:21 PM
It's not unreachable code you numpty. That's the worst definition ever.

This is unreachable code.

:

function doSomething( int x )
{
    if (x == 1)
        return 1;
    else
        return 0;

    return 0; // << unreachable, never ever in a million years will this get executed.
}


His print statement gets executed, which means it's not unreachable code.


This is more like what he should be doing:


:
for (int i=0; i < numberArray.length; i++) // Loop through all of the array elements
{
    if ( (numberArray[i] % 2) == 0) // if the current element is divisible by two and has no remainder (an even number)
    {
        if (numberArray[i] < 412) // if the number is less than 412
        {
            print numberArray[i]; // print it out
        }
    }
}


This can also be optimized further by performing the modulo check at the same time as the number size by using an AND operator


:

for (int i=0; i < numberArray.length; i++) // Loop through all of the array elements
{
    if ( ((numberArray[i] % 2) == 0) && numberArray[i] < 412 ) // if the current element is divisible by two and has no remainder
    {                                                          // (an even number)  AND (&&) is less than 412
        print numberArray[i]; // print it out
    }
}
: Re: Coding Help
: Deque April 03, 2012, 03:32:54 PM
I don't see any ambiguity in the task.

as soon as your program hits 412 it will stop

That is exactly what it shall do. It says:

but only up to the number "412" (not including it) Stop there and ignore all numbers past 412 in the sequence
: Re: Coding Help
: xor April 03, 2012, 06:02:15 PM
I'm basing it on the assumption that his native language isn't English and the fact that he also said:  (Note there will be numbers over 412.)