Newbie. Need help

Joshua Landau joshua.landau.ws at gmail.com
Mon Jul 8 09:02:04 EDT 2013


On 8 July 2013 13:05, Sandile Mnukwa <sandile.mnukwa at gmail.com> wrote:
> Hi Joshua,

Hello.

You replied off-list (to me only, not to Python-list). I imagine this
was a mistake, so I'm posting to Python-list again. If this wasn't a
mistake, then I apologize and suggest telling people when you mean to
reply off-list.

Also, you top-posted, which isn't the right thing to do. This means
you wrote your reply above the copy of the text you were replying to
-- you should write below. You should also delete all of the parts
that you aren't responding to, so people know what you are talking
about.

> Thanks for the help so far.
> Just been panicking about learning the language...
>
> I have been given a task to do. I am a new to programming and Python.
> My task is to :
> -Create a function that is called from the main function, that accepts a number as a parameter and determines if the number is even or odd.
> the next one is,

You have done:

def main():
    pass

So you know how to define a *main function*. This should take a parameter.

Do you know how to make a function accept a parameter?

Once you have that parameter, you need to check whether it is even or
odd. If you don't know how to do this, check the link I gave in my
last post or try a Google search for "python check number even odd".

Knowing how to look for answers to simple (and later complicated)
problems is important to being a good programmer.

> -To create another function that generates a random number, and basically when i print this function, it should give me a list of random number separated by a "," commma or in a list.

OK, that's good. You know how to generate random numbers
(random.randrange), so now you want to define a function.

def generate_numbers():
    ...

You want to *return* a *list* of *random numbers*:

    list_of_random_numbers = []

    for i in range(10):
        random_number = <make a random number> #  You fill this in

        list_of_random_numbers.append(random_number)

then you want to *return* the *list_of_random_numbers*. Do you know
how to return values?

> -And lastly to enhance my script to generate 10 random EVEN numbers and write them to a .txt file. (This task is the most important of them all)

You know how to generate 10 random even numbers. I know you know this
because you have done:
>     for i in range(10):
>         print random.randrange(0, 101, 2)

You should assign each random number to a name (using "=").
*Inside* your loop, you want to do SOME_FILE.write(str(RANDOM_NUMBER))

> What I have done so far.
>
> import random
>
> if __name__ == '__main__':
>     main()
>     for i in range(10):
>         print random.randrange(0, 101, 2)

When I said to put this in the main function, I meant the *function*,
not here. This works, but it isn't what people normally do. See how
you call "main()" inside here? Thus, when you have the

>     for i in range(10):
>         print random.randrange(0, 101, 2)

inside "main()" it will get run anyway. Does this make sense?

> with open ("C:\Users\Kenz09\Documents\Myproj.txt", "w") as f:
>     print f

When I said use "with", I mean use "with" *everywhere*, not just here.
So you should use this same pattern in the other places you open
files.

You will need to indent the *whole* of the code that requires use of
the file "f" when you do this.

> f = open("C:\Users\Kenz09\Documents\Myproj.txt", "a");
> print f
>
> value = (
> random.randrange (0, 101, 2),
> random.randrange(0, 201, 2),
> random.randrange(0, 301, 2)
> )
>
> random_numbers[0]
> random_numbers[1]
> random_numbers[2]
>
> print f.write(str(value))
> print f.write(str(value1))
> print f.write(str(value2))
> f.close()

Remember that I'm telling you this so that you know what to do, not so
you can take the code I give you. I will not give you complete
solutions -- if you ask what "5 + 5" is I will show you how to work
out "4 + 4", and then you can apply that to the original problem.



More information about the Python-list mailing list