Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #50147 > unrolled thread
| Started by | Kenz09 <sandile.mnukwa@gmail.com> |
|---|---|
| First post | 2013-07-08 05:01 -0700 |
| Last post | 2013-07-08 16:17 -0400 |
| Articles | 4 — 3 participants |
Back to article view | Back to comp.lang.python
How do I write a script to generate 10 random EVEN numbers and write them to a .txt file? Kenz09 <sandile.mnukwa@gmail.com> - 2013-07-08 05:01 -0700
Re: How do I write a script to generate 10 random EVEN numbers and write them to a .txt file? Dave Angel <davea@davea.name> - 2013-07-08 08:27 -0400
Re: How do I write a script to generate 10 random EVEN numbers and write them to a .txt file? Joshua Landau <joshua.landau.ws@gmail.com> - 2013-07-08 14:10 +0100
Re: How do I write a script to generate 10 random EVEN numbers and write them to a .txt file? Dave Angel <davea@davea.name> - 2013-07-08 16:17 -0400
| From | Kenz09 <sandile.mnukwa@gmail.com> |
|---|---|
| Date | 2013-07-08 05:01 -0700 |
| Subject | How do I write a script to generate 10 random EVEN numbers and write them to a .txt file? |
| Message-ID | <25284710-bf3f-4e10-bac2-0ba19099f653@googlegroups.com> |
Hi, 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,
-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.
-And lastly to enhance my script to generate 10 random EVEN numbers and write them to a .txt file.
This what I have done so far.
import random
if __name__ == '__main__':
main()
for i in range(10):
print random.randrange(0, 101, 2)
with open ("C:\Users\Kenz09\Documents\Myproj.txt", "w") as f:
print f
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()
[toc] | [next] | [standalone]
| From | Dave Angel <davea@davea.name> |
|---|---|
| Date | 2013-07-08 08:27 -0400 |
| Message-ID | <mailman.4382.1373286446.3114.python-list@python.org> |
| In reply to | #50147 |
On 07/08/2013 08:01 AM, Kenz09 wrote:
> Hi, 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,
>
> -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.
>
> -And lastly to enhance my script to generate 10 random EVEN numbers and write them to a .txt file.
>
One of your classmates has already posted the question. However, you
win the prize for a better subject line. Or are you the same student,
changing your name and wasting our time by starting a new thread.
> This what I have done so far.
Where's your main function?
>
> import random
>
> if __name__ == '__main__':
> main()
> for i in range(10):
> print random.randrange(0, 101, 2)
Why is everything at top level, and not inside the function(s) ?
>
> with open ("C:\Users\Kenz09\Documents\Myproj.txt", "w") as f:
> print f
> f = open("C:\Users\Kenz09\Documents\Myproj.txt", "a");
That semicolon comes from another language. Very seldom any need for it
in Python.
> print f
>
> value = (
> random.randrange (0, 101, 2),
> random.randrange(0, 201, 2),
> random.randrange(0, 301, 2)
> )
The reason the instructor told you to do 10 is probably to discourage
you from typing separate statements for all 10. You're undoubtedly
supposed to use a loop.
>
> random_numbers[0]
> random_numbers[1]
> random_numbers[2]
When you asigned it, you called it value, but now you expect it to have
another name?
>
> print f.write(str(value))
> print f.write(str(value1))
> print f.write(str(value2))
> f.close()
>
Why not start by doing the first statement of the assignment, and if you
can't make it work, or don't understand some part of the question, ask
again? Show your work, not just a bunch of random statements, most of
which won't execute.
If I were wording that first question, I'd say something like:
"""
Create a function that accepts a integer as a parameter and determines
if the number is even or odd, returning a boolean value accordingly.
Call that function multiple times from the main function, with a variety
of values, indicating for each whether it's even or odd.
"""
--
DaveA
[toc] | [prev] | [next] | [standalone]
| From | Joshua Landau <joshua.landau.ws@gmail.com> |
|---|---|
| Date | 2013-07-08 14:10 +0100 |
| Message-ID | <mailman.4387.1373289093.3114.python-list@python.org> |
| In reply to | #50147 |
On 8 July 2013 13:27, Dave Angel <davea@davea.name> wrote: > One of your classmates has already posted the question. However, you win > the prize for a better subject line. Or are you the same student, changing > your name and wasting our time by starting a new thread. Considering the body of the question and his GMail user icon thingy are identical, I think that's a rigged question.
[toc] | [prev] | [next] | [standalone]
| From | Dave Angel <davea@davea.name> |
|---|---|
| Date | 2013-07-08 16:17 -0400 |
| Message-ID | <mailman.4395.1373314653.3114.python-list@python.org> |
| In reply to | #50147 |
On 07/08/2013 09:10 AM, Joshua Landau wrote: > On 8 July 2013 13:27, Dave Angel <davea@davea.name> wrote: >> One of your classmates has already posted the question. However, you win >> the prize for a better subject line. Or are you the same student, changing >> your name and wasting our time by starting a new thread. > > Considering the body of the question and his GMail user icon thingy > are identical, I think that's a rigged question. > I didn't notice that the actual gmail address was the same; somehow when I saw it prefixed by the Sanza101 and Kenz09 it looked different. However, the body was entirely different, thoroughly rephrased. For example, first question in each, respectively, was: Create another function that generates a random number (You will have to import the relevant library to do this) 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, -- DaveA
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web