Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!goblin2!goblin.stu.neva.ru!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'python,': 0.02; 'subject:Python': 0.06; 'string': 0.09; 'append': 0.09; 'beginners': 0.09; 'editor.': 0.09; 'main()': 0.09; 'php,': 0.09; 'python:': 0.09; 'try:': 0.09; 'way:': 0.09; 'runs': 0.10; 'cc:addr:python-list': 0.11; 'subject:Help': 0.11; 'python': 0.11; 'def': 0.12; 'wrote': 0.14; 'language.': 0.14; 'random': 0.14; 'books': 0.15; '>>': 0.16; 'guessing': 0.16; 'simplest': 0.16; 'simplicity.': 0.16; 'skip:n 50': 0.16; 'utc,': 0.16; "\xc2\xa0i'm": 0.16; '\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0': 0.16; 'elements': 0.16; 'sat,': 0.16; 'skip:# 20': 0.16; 'student': 0.16; 'language': 0.16; 'wrote:': 0.18; 'code.': 0.18; 'trying': 0.19; '<': 0.19; 'code,': 0.22; 'import': 0.22; 'email addr:gmail.com>': 0.22; 'shell': 0.22; 'cc:addr:python.org': 0.22; 'print': 0.22; 'char': 0.24; 'finally,': 0.24; 'instead.': 0.24; 'integer': 0.24; 'number)': 0.24; 'videos,': 0.24; 'cc:2**0': 0.24; 'sort': 0.25; '>': 0.26; 'this:': 0.26; 'tutorials': 0.26; 'post': 0.26; 'header:In- Reply-To:1': 0.27; 'correct': 0.29; 'character': 0.29; '(like': 0.30; 'characters': 0.30; 'statement': 0.30; 'subject:please': 0.30; 'message-id:@mail.gmail.com': 0.30; 'skip:( 20': 0.30; "i'm": 0.30; 'url:mailman': 0.30; 'code': 0.31; 'getting': 0.31; 'comments': 0.31; 'that.': 0.31; 'url:wiki': 0.31; 'sep': 0.31; 'stands': 0.31; 'url:wikipedia': 0.31; 'languages': 0.32; 'run': 0.32; 'another': 0.32; 'text': 0.33; 'open': 0.33; 'url:python': 0.33; 'running': 0.33; 'plain': 0.33; 'sense': 0.34; 'maybe': 0.34; 'knowledge': 0.35; 'subject: (': 0.35; 'subject:with': 0.35; "can't": 0.35; 'common': 0.35; 'knows': 0.35; 'editor': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'version': 0.36; 'url:listinfo': 0.36; "didn't": 0.36; 'url:org': 0.36; 'list': 0.37; 'list.': 0.37; 'skip:& 10': 0.38; 'pm,': 0.38; 'little': 0.38; 'skip:& 20': 0.39; 'extremely': 0.39; 'generating': 0.39; 'either': 0.39; 'skip:p 20': 0.39; 'url:mail': 0.40; 'how': 0.40; 'even': 0.60; 'skip:u 10': 0.60; 'anytime': 0.60; 'most': 0.60; 'hope': 0.61; 'skip:* 10': 0.61; 'simply': 0.61; 'you.': 0.62; "you'll": 0.62; 'email addr:gmail.com': 0.63; 'kind': 0.63; 'skip:n 10': 0.64; 'more': 0.64; 'finally': 0.65; 'to:addr:gmail.com': 0.65; 'number:': 0.66; 'here': 0.66; 'anything.': 0.68; 'of:': 0.68; 'watching': 0.68; 'overall': 0.69; 'skip:r 30': 0.69; '\xc2\xa0\xc2\xa0': 0.74; 'promoting': 0.78; 'glad': 0.83; 'exercise.': 0.84; 'homework': 0.84; 'number):': 0.84; 'subject:picture': 0.84; 'tolerate': 0.84; 'url:imgur': 0.84; 'joel': 0.91; '8bit%:42': 0.95; '2013': 0.98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=zi9tbdO/qFuYmoq9COjDYuGVS0rf71u0Q64qc0fNpkw=; b=NvMaOzpj6HaiIoOqilPP6xvH/OX9Xl8QyjFJkJJBqGC24qmfPa1opS1JCeMZPu+0Is OUjR1eOnrFfJvN36yNwcvqMDYtf42iz0JGQdfvI/LtJYVPBFI8DzuPpRzjZTYv0NtdwK +ygleCSi1pNs91MSyDSBcrxZW2DQVWmUC1s/REkEvYaLVE6IJQeFJp6bTCdO4A2OI8bZ X+erLzbc2cBS/wLpNEewH+06eRHNVMm0OfF9JnMSCWktK/zis2OBxESDCrhy5DeNvV6q 9/lUg+5edI9A+SWB5uMl485JPc38DhpIWk0zKOb0qyKHeRsz1g/mzVetRgC60HHVKDXx 2f0Q== MIME-Version: 1.0 X-Received: by 10.220.181.136 with SMTP id by8mr12794805vcb.11.1380387018016; Sat, 28 Sep 2013 09:50:18 -0700 (PDT) In-Reply-To: <1f5d2dd8-877d-4d80-b3c3-d5a048556061@googlegroups.com> References: <7eb1cc5a-a25c-448e-9131-2f8b3ee60f96@googlegroups.com> <1f5d2dd8-877d-4d80-b3c3-d5a048556061@googlegroups.com> Date: Sat, 28 Sep 2013 12:50:17 -0400 Subject: Re: Help me with Python please (picture) From: Joel Goldstick To: dvghana@gmail.com Content-Type: multipart/alternative; boundary=047d7bf0f67adc8dbc04e7746532 Cc: "python-list@python.org" X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 235 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1380387020 news.xs4all.nl 15984 [2001:888:2000:d::a6]:53749 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:54955 --047d7bf0f67adc8dbc04e7746532 Content-Type: text/plain; charset=UTF-8 On Sat, Sep 28, 2013 at 12:17 PM, wrote: > On Saturday, September 28, 2013 12:43:42 AM UTC, jae...@gmail.com wrote: > > http://imgur.com/E6vrNs4 > > > > > > > > > > > > Can't seem to be getting an output. > > All the comments about using an image to ask for help over here is > extremely valid so I hope you accept it in good faith. I am a noob like you > so I can tolerate it and see if I can help you. > > So here we go: > 1. random.randit will only return an integer but it sounds to me like you > are trying to return one of the elements in "chars" > > If my understanding is correct try using random.choice instead. > > To return a random character from the alphabets you can try: > > >> import string > >> char = random.choice(string.ascii_lowercase) > #string.ascii_uppercase for uppercace > > 2. you may not need the main() function. > and you didn't have any 'print' statement so when you run the code you > won't see anything. You are simply generating random characters and > throwing them away > > try: > print (random_characters(8)) > > 3. but if you run the code as it stands it won't generate 8 random > charaters so you'll actually have to be appending it on a list. > > So instead of: > new_string = '' > try: > new_string = [] > > 4. Finally, join the elements in the list once they are generated like > this: > return "".join(new_string) > but don't forget to append each character anytime the loop runs this way: > new_string.append(random.choice(string.ascii_lowercase)) > > Overall I wrote my own version of the code and this is what I got: > > > ****************** > import string > import random > > def random_characters(number): > i = 0 > new_string = [] > > while (i < number) : > new_string.append(random.choice(string.ascii_lowercase)) > i = i + 1 > return "".join(new_string) > > > print(random_characters(3)) > ******* > -- > https://mail.python.org/mailman/listinfo/python-list > I'm guessing this is homework since its kind of a contrived exercise. If you do any reading of python tutorials or books for beginners I don't think you will see this sort of loop used in python: while i < number: do_something ... i += 1 This is even weirder: while (i < number): Its not that you can't do that. But it is much more common to see in other languages (like C or maybe PHP, others ?). So either the instructor is promoting this kind of loop because he knows a little about another language and not so much about python, or the student has some knowledge of another language. I prefer: for i in range(number): do_something... and finally plus + on the calling out of using images to post code. I'm guessing the OP is a computer user in the sense of running word, or watching you-tube videos, etc. Most non-programmers never used a plain text editor. If you want to learn how to write code, and you are a beginner, use the simplest text editor on your computer, learn to open a shell and run python interactively and run programs that you saved with your text editors. Be glad for simplicity. And happy you don't write code on these: http://en.wikipedia.org/wiki/Hollerith_cards -- Joel Goldstick http://joelgoldstick.com --047d7bf0f67adc8dbc04e7746532 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable



On Sat, Sep 28, 2013 at 12:17 PM, <dvghana@gmail.com> wrote:
On Satu= rday, September 28, 2013 12:43:42 AM UTC, jae...@gmail.com wrote:
> http://imgur.co= m/E6vrNs4
>
>
>
>
>
> Can't seem to be getting an output.

All the comments about using an image to ask for help over here is ex= tremely valid so I hope you accept it in good faith. I am a noob like you s= o I can tolerate it and see if I can help you.

So here we =C2=A0go:
1. random.randit will only return an integer but it sounds to me like you a= re trying to return one of the elements in "chars"

If my understanding is correct try using random.choice instead.

To return a random character from the alphabets you can try:

>> import string
>> char =3D random.choice(string.ascii_lowercase)
=C2=A0 =C2=A0#string.ascii_uppercase for uppercace

2. you may not need the main() function.
and you didn't have any 'print' statement so when you run the c= ode you won't see anything. You are simply generating random characters= and throwing them away

try:
print (random_characters(8))

3. but if you run the code as it stands it won't generate 8 random char= aters so you'll actually have to be appending it on a list.

So instead of:
new_string =3D ''
try:
new_string =3D []

4. Finally, join the elements in the list once they are generated like this= :
=C2=A0 =C2=A0 return "".join(new_string)
but don't forget to append each character anytime the loop runs this wa= y:
=C2=A0 =C2=A0 new_string.append(random.choice(string.ascii_lowercase))

Overall I wrote my own version of the code and this is what I got:


******************
import string
import random

def random_characters(number):
=C2=A0 =C2=A0 i =3D 0
=C2=A0 =C2=A0 new_string =3D []

=C2=A0 =C2=A0 while (i < number) :
=C2=A0 =C2=A0 =C2=A0 =C2=A0 new_string.append(random.choice(string.ascii_lo= wercase))
=C2=A0 =C2=A0 =C2=A0 =C2=A0 i =3D i + 1
=C2=A0 =C2=A0 return "".join(new_string)


print(random_characters(3))
*******
--
https://mail.python.org/mailman/listinfo/python-list

=C2= =A0I'm guessing this is homework since its kind of a contrived exercise= .=C2=A0 If you do any reading of python tutorials or books for beginners I = don't think=C2=A0 you will see this sort of loop used in python:

=C2=A0=C2=A0 while i < number:
<= /div>
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 do_something= ...
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 i += =3D 1

This is even weirder:

=C2=A0 while (i < number):
=C2= =A0=C2=A0


Its not that you can't do that.=C2=A0 But it is much more = common to see in other languages (like C or maybe PHP, others ?).=C2=A0 So = either the instructor is promoting this kind of loop because he knows a lit= tle about another language and not so much about python,=C2=A0 or the stude= nt has some knowledge of another language.

I prefer:
=C2=A0 for i in range(number):
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 do_something...

<= div class=3D"gmail_extra">
and finally

plus + on the calling out of using images to post code.=C2=A0 I'm g= uessing the OP is a computer user in the sense of running word, or watching= you-tube videos, etc.=C2=A0 Most non-programmers never used a plain text e= ditor. =C2=A0 If you want to learn how to write code, and you are a beginne= r, use the simplest text editor on your computer, learn to open a shell and= run python interactively and run programs that you saved with your text ed= itors.=C2=A0 Be glad for simplicity.=C2=A0 And happy you don't write co= de on these: http:= //en.wikipedia.org/wiki/Hollerith_cards


-= -
--047d7bf0f67adc8dbc04e7746532--