Newbie. Need help
Alister
alister.ware at ntlworld.com
Mon Jul 8 07:51:15 EDT 2013
On Mon, 08 Jul 2013 01:53:06 -0700, Sanza101 wrote:
> 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()
As this is course work I wont give you answers just pointers to help.
from the wording of the question your teacher is probably more interested
in you understanding how to define functions.
so from the design requirements you will need something like
def get_random():
#your code foes here
def is_even(number):
#you code here
you then need to loop through the code writing to file each time you get
an even result.
a for loop is probably not the best type of loop for this task.
once you have some more code I am sure we will help further if you get
stuck with any specifics.
--
The computer is to the information industry roughly what the
central power station is to the electrical industry.
-- Peter Drucker
More information about the Python-list
mailing list