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


Groups > comp.lang.python > #54955

Re: Help me with Python please (picture)

References <7eb1cc5a-a25c-448e-9131-2f8b3ee60f96@googlegroups.com> <1f5d2dd8-877d-4d80-b3c3-d5a048556061@googlegroups.com>
Date 2013-09-28 12:50 -0400
Subject Re: Help me with Python please (picture)
From Joel Goldstick <joel.goldstick@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.421.1380387020.18130.python-list@python.org> (permalink)

Show all headers | View raw


[Multipart message — attachments visible in raw view] - view raw

On Sat, Sep 28, 2013 at 12:17 PM, <dvghana@gmail.com> 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

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


Thread

Help me with Python please (picture) jae655@gmail.com - 2013-09-27 17:43 -0700
  Re: Help me with Python please (picture) Gary Herron <gary.herron@islandtraining.com> - 2013-09-27 18:11 -0700
  Re: Help me with Python please (picture) Dave Angel <davea@davea.name> - 2013-09-28 01:18 +0000
  Re: Help me with Python please (picture) Denis McMahon <denismfmcmahon@gmail.com> - 2013-09-28 02:18 +0000
  Re: Help me with Python please (picture) John Ladasky <john_ladasky@sbcglobal.net> - 2013-09-27 21:03 -0700
  Re: Help me with Python please (picture) Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-09-28 05:18 +0000
    Re: Help me with Python please (picture) Chris Angelico <rosuav@gmail.com> - 2013-09-28 15:46 +1000
  Re: Help me with Python please (picture) dvghana@gmail.com - 2013-09-28 09:17 -0700
    Re: Help me with Python please (picture) Joel Goldstick <joel.goldstick@gmail.com> - 2013-09-28 12:50 -0400
      Re: Help me with Python please (picture) Bob Martin <bob.martin@excite.com> - 2013-09-29 08:07 +0100
    Re: Help me with Python please (picture) Dave Angel <davea@davea.name> - 2013-09-28 17:16 +0000

csiph-web