Newbie. Need help

Joshua Landau joshua.landau.ws at gmail.com
Mon Jul 8 12:28:55 CEST 2013


On 8 July 2013 09:53, Sanza101 <sandile.mnukwa at gmail.com> wrote:
> I just started using Python recently, and i need help with the following: Please assist.

Rather than saying you want help with "Please assist", why don't you
ask a question?

I find when people start their post with "I need help, please help"
they forget that (a) what else would we try to do and (b) to state
what they need help with.

> 1.      Create another function that generates a random number (You will have to import the relevant library to do this)
> 2.      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
> 3.      Now enhance your script to generate 10 random even numbers and write them to a file

It's normally nice to state when something's a homework. It puts you
on much better, more honest terms than if you don't. We won't blank
you if you tell us or anything.

> So far what i have done is:
> import random
> def main():
>     pass
>
> if __name__ == '__main__':
>     main()
> for evenNumber in range (0, 20, 2):
>     print random.randrange(0, 101, 2)
>
> f = open("C:\Users\Documents\Myproj.txt", "w");
> print f
> f = open("C:\Users\Documents\Myproj.txt", "a");
> print f
>
> value = random.randrange(0, 101, 2, )
> value1 = random.randrange(0, 201, 2, )
> value2 = random.randrange(0, 301, 2, )
>
> myString = str(value)
> myString1 = str(value1)
> myString2 = str(value2)
>
> print f.write(myString)
> print f.write(myString1)
> print f.write(myString2)
> f.close()

OK... so? What do you need help with?

I'll guide you through what you've done, and why it doesn't do what you want.

> import random

So far so good.

> def main():
>     pass

This does.. nothing.
You probably want to put everything inside your main function, so it
is called only if "__name__ == '__main__'". This is done so that is
people "import yourmodule" then they won't immediately have lots of
things happening that aren't useful -- like putting numbers in files,
but they can still use your functions that you write.

> if __name__ == '__main__':
>     main()

That's fine.

> for evenNumber in range (0, 20, 2):
>     print random.randrange(0, 101, 2)

You do "for evenNumber in ..." but never use "evenNumber". you should
write "for i in range(10):" which is a much better way of saying "do
this 10 times".

Additionally, no-one in python seems to use thisWayOfNamingThings.
Whilst it's not _wrong_, per se, it's not standard either. You'll be
happier in the long run if you learn to use this_way_of_naming_things.

You were asked to "Create another function that generates a random
number". I'm not really sure what this means, but it sounds to me like
you want to do:

    def generate_random_number():
        ...

which I would just write: "generate_random_number = random.randrange".
Yes, just use what you already have.

> f = open("C:\Users\Documents\Myproj.txt", "w");

Oh no! What is this ";" on the end? DESTROY IT NOW.

> print f

This just prints "<open file 'C:\some\place', mode 'w' at
0x7fb0361945d0>". Why did you do this?

Also, the recommended way to open a file and use it is like this:

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

This makes sure you don't forget to close it, and it handles some
stuff really cleverly and well. You don't need to know what it all
means yet, but just know that for "open" it does The Right Thing™.

> f = open("C:\Users\Documents\Myproj.txt", "a");
> print f

Exactly the same comments.

> value = random.randrange(0, 101, 2, )
> value1 = random.randrange(0, 201, 2, )
> value2 = random.randrange(0, 301, 2, )

Oh.. kay. Why do you have a trailing ", "? Just do:

> value = random.randrange(0, 101, 2)
> value1 = random.randrange(0, 201, 2)
> value2 = random.randrange(0, 301, 2)

Also, why are they "value", "value1" and "value2"? Those names make no
sense. You could always put them in a tuple (like a list of things):

random_numbers = (
    random.randrange(0, 101, 2),
    random.randrange(0, 201, 2),
    random.randrange(0, 301, 2)
)

and then you can index them like "random_numbers[0]" instead of
"value", "random_numbers[1]" instead of "value1" and
"random_numbers[2]" instead of "value2". I don't know.

> myString = str(value)
> myString1 = str(value1)
> myString2 = str(value2)

Urm... "myString"? What does that even mean? If it were me, I'd
convert when I need to:

> print f.write(str(value))
> print f.write(str(value1))
> print f.write(str(value2))

Also, what do you think the "print" does here? It doesn't print the
value. You have no reason for the print, and should remove it.

> f.close()

If you use "with open(...) as f:" you do not need to close f, btw.


So back to the tasks:


> 1.      Create another function that generates a random number (You will have to import the relevant library to do this)

As before, this is a stupid question. If you know what it means, do explain.


> 2.      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

There are only four

- Create a function

How do you do this? This is easy, so do tell.

- that is called from the main function

So you want to run the function inside main(); how do you do this?

- that accepts a number as a parameter

Do you know how to make functions take parameters?

- determines if the number is even or odd

How do you think you would do this? Hint:
http://stackoverflow.com/questions/12754680/modulo-operator-in-python


> 3.      Now enhance your script to generate 10 random even numbers and write them to a file

You know how to generate 10 random numbers. You know how, given a
number to write it to a file. So, in your loop, write each number to a
file.



More information about the Python-list mailing list