A newbie that needs some HELP

Peter Hansen peter at engcorp.com
Mon Nov 26 03:31:52 CET 2001


The News wrote:
> 
> monty = []
> number = input("How many Monty's are there?")
> for x in range(1,number):
>     mln = input("Name a Monty?")
>     monty.append(aln)
> print monty
> 
> It gets through it but prints out some wierd stuff along with each listed
> monty.  Can someone tell me what I'm doing wrong?  Also, when I try to get
> it to repeat x at the end of "Name a Monty #", x  it gives me an error.  How
> could I get around that?

range() the way you have it will return one less than
the number you probably want.  Use range(0,number) or
just range(number) instead.

Use raw_input() instead of input(), which evaluates its
input as an expression (returning a value, not a string,
if possible).

When you print the list that way, you get a printable
representation of the data, which might be the source
of the "weird stuff".  Maybe you want something more 
like this instead:

  print ', '.join(monty)

Finally, you can't use the comma-notation to join strings
except with the print statement (which has some unique
behaviour in Python).  Use the format operator instead,
as in raw_input("Name a Monty #%s" % x).

-- 
----------------------
Peter Hansen, P.Eng.
peter at engcorp.com



More information about the Python-list mailing list