[Tutor] arrays, while loops
bob gailer
bgailer at gmail.com
Mon Feb 20 19:59:39 CET 2012
Thanks for your response. Please always reply-all so a copy goes to the
tutor list. I'm cc'ing this to that list.
On 2/19/2012 10:13 AM, Deborah Knoll wrote:
> This is for a class - I wasn't trying to hide that fact. I didn't
> want someone to write this for me,
I understand and appreciate that. I was commenting more to the other
list responders.
> just give some help so I could learn, which I did get. As for this
> being too advanced, it's a beginner Program Logic course and I think
> the code is suppose to be written fairly simply
Something got derailed - the question you posed requires prior learning;
your attempts don't show that you had that learning.
> - it's an online course which makes it harder but I am learning.
Would you provide a link to the course so we could see it?
> I didn't know of your policy but from now on I will keep it to more
> simple quesions instead of the whole program.
That's always a good idea. Your program wasn't that big. But there was
so much "wrong" with it - that made me question the relevance of the
assignment.
> I do want to say thanks to everyone for the help - I'm excited to be
> learning more.
I like hearing that. Please also note that we like to see our answers
interspersed with your comments. Please do the same when replying to us.
Also delete any part of the email to which you reply that is not
relevant to your reply. And please reply. We actually enjoy helping.
Some "generic" comments:
1) when working with a fixed-lengthobject (e.g.a list of length 7) start
by defining a constant and then refer to that constant. e.g.:
SIZE = 7
amounts = [0]*SIZE
2) when obtaining user input that should be a numberbut might not it is
essential that you (in one of several ways) anticipate and handle "bad"
input. One way is to use isdigit() to ensure that all the characters
entered are digits. This assumes that inputs with a decimal point are
not allowed, but your problem does not rule them out - it's just harder
to detect "bad" input if decimal points are allowed. So let's assume
integers only for now. Let's create a function whose job is to get an
integer and keep trying till the user enters one. We might as well throw
in the range check as this is the best place to do that.
def getInteger():
while True: # this keeps asking for in put until user get it right
x = raw_input ("Enter an integer amount between 1 and 1000: ") #
spell out all the requirements
if x.isdigit():
y = int(x)
if y >= 1 and y <= 1000:
return y
else:
print(x + " is not between 1 and 1000")
else:
print(x + " is not an integer")
print getInteger()
I wrote this as a stand-alone program so I could test it by itself, and
added the last line to actually run the test.
Once the getInteger function is working then you can add the other logic.
Another approach is using try and except as others have mentioned.
However I doubt whether your course has introduced exception handling.
--
Bob Gailer
919-636-4239
Chapel Hill NC
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20120220/37217a99/attachment.html>
More information about the Tutor
mailing list