Help me with Python please (picture)

Joel Goldstick joel.goldstick at gmail.com
Sat Sep 28 12:50:17 EDT 2013


On Sat, Sep 28, 2013 at 12:17 PM, <dvghana at gmail.com> wrote:

> On Saturday, September 28, 2013 12:43:42 AM UTC, jae... at 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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20130928/6c83a6a4/attachment.html>


More information about the Python-list mailing list