Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #50149

Re: How do I write a script to generate 10 random EVEN numbers and write them to a .txt file?

Path csiph.com!usenet.pasdenom.info!news.albasani.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'python.': 0.02; 'value,': 0.04; '-to': 0.05; '"""': 0.07; 'indicating': 0.07; 'level,': 0.07; 'subject:file': 0.07; '__name__': 0.09; 'f.close()': 0.09; 'function,': 0.09; 'main()': 0.09; 'name?': 0.09; 'parameter': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'statements': 0.09; 'subject:script': 0.09; 'subject:How': 0.10; 'language.': 0.14; 'question.': 0.14; 'random': 0.14; 'posted': 0.15; '","': 0.16; '"w")': 0.16; "'__main__':": 0.16; '(0,': 0.16; '-create': 0.16; '.txt': 0.16; '2),': 0.16; 'accepts': 0.16; 'boolean': 0.16; 'determines': 0.16; 'function(s)': 0.16; 'function?': 0.16; 'loop.': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'semicolon': 0.16; 'separated': 0.16; 'statements,': 0.16; 'student,': 0.16; 'subject: \n ': 0.16; 'subject:.txt': 0.16; 'subject:random': 0.16; "where's": 0.16; 'wording': 0.16; 'accordingly.': 0.16; 'wrote:': 0.18; 'do.': 0.18; 'basically': 0.19; 'typing': 0.19; 'work,': 0.20; 'programming': 0.22; 'import': 0.22; 'separate': 0.22; 'print': 0.22; 'header:User-Agent:1': 0.23; 'integer': 0.24; 'subject: .': 0.24; 'file.': 0.24; 'script': 0.25; 'task': 0.26; 'header:X -Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'am,': 0.29; 'skip:( 40': 0.30; 'statement': 0.30; '-and': 0.31; 'bunch': 0.31; 'subject:numbers': 0.31; 'probably': 0.32; 'supposed': 0.32; 'another': 0.32; 'open': 0.33; "i'd": 0.34; "can't": 0.35; 'something': 0.35; 'but': 0.35; 'returning': 0.36; 'done': 0.36; 'doing': 0.36; 'next': 0.36; 'subject:?': 0.36; 'hi,': 0.36; 'should': 0.36; 'changing': 0.37; 'list': 0.37; 'list.': 0.37; 'starting': 0.37; 'question,': 0.38; 'to:addr :python-list': 0.38; 'expect': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'called': 0.40; 'even': 0.60; 'number,': 0.60; 'most': 0.60; 'new': 0.61; 'numbers': 0.61; "you're": 0.61; 'first': 0.61; 'times': 0.62; 'show': 0.63; 'name': 0.63; 'our': 0.64; 'subject': 0.69; 'prize': 0.74; '301,': 0.84; 'again?': 0.84; 'seldom': 0.84; 'undoubtedly': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Dave Angel <davea@davea.name>
Subject Re: How do I write a script to generate 10 random EVEN numbers and write them to a .txt file?
Date Mon, 08 Jul 2013 08:27:06 -0400
References <25284710-bf3f-4e10-bac2-0ba19099f653@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host 174.32.174.33
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130623 Thunderbird/17.0.7
In-Reply-To <25284710-bf3f-4e10-bac2-0ba19099f653@googlegroups.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.4382.1373286446.3114.python-list@python.org> (permalink)
Lines 83
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1373286446 news.xs4all.nl 15870 [2001:888:2000:d::a6]:55851
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:50149

Show key headers only | View raw


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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web