Newbie. Need help

S

Sanza101

I just started using Python recently, and i need help with the following: Please assist.

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

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()
 
J

Joshua Landau

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.

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

Joshua Landau

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,962
Messages
2,570,134
Members
46,690
Latest member
MacGyver

Latest Threads

Top