Re: New to programming question
Joal Heagney wrote: Steve Holden wrote: I suppose this would be far too easy to understand, then: pr =['Guess my name', 'Wrong, try again', 'Last chance'] for p in pr: name = raw_input(p+": ") if name == "Ben": print "You're right!" break else: print "Loser: no more tries for you" regards Steve THIS is why I like python! There's always a simple, easy to understand way to do something. If it looks complex, then there must me something wrong. Joal And now that I've looked at the documentation of the for loop, I understand it as well! :) The following explaination is for Ben, so he knows what's going on. From the documentation, with a little rewriting. "The for statement is used to iterate over the elements of a sequence (such as a string, tuple or list) or other iterable object: for target_list "in" expression_list: else: The expression list is evaluated once and should yield a sequence(such as a string, tuple, list or iterator object). Each item in the sequence is assigned to the target_list variable in turn. Then the "do this first" instructions are then executed once for each item in the sequence. When the items are exhausted (which is immediately when the sequence is empty), the "now do this last" instructions in the else statement, if present, are executed, and the loop terminates. A break statement executed in the first section terminates the WHOLE loop without executing the else clause. A continue statement executed in the first stage skips the rest of these instructions for that loop and continues with the next item, or with the else clause if there was no next item." So copying Steve's example: >> pr =['Guess my name', 'Wrong, try again', 'Last chance'] >> for p in pr: >> name = raw_input(p+": ") >> if name == "Ben": >> print "You're right!" >> break >> else: >> print "Loser: no more tries for you" This allows us to execute the else clause if the name is guessed incorrectly three times. However, if the name is guessed correctly, then the break statement pulls us completely out of the loop without executing the else clause. My original example attempted to do this by splitting the loop up into a series of different cases because I was unaware of this additional behaviour with the for loop expression. Steve's method = much better. Joal -- http://mail.python.org/mailman/listinfo/python-list
Re: New to programming question
Steve Holden wrote: Joal Heagney wrote: Bengt Richter wrote: On Fri, 01 Apr 2005 07:46:41 GMT, Joal Heagney <[EMAIL PROTECTED]> wrote: Oh goddammmni. I seem to be doing this a lot today. Look below for the extra addition to the code I posted. Joal Heagney wrote: Here's my contribution anycase: count = 0 # Get first input name = raw_input("Guess my name: ") # Give the sucker two extra goes while count < 2: # Check the value of name if name == 'Ben': print "You're right!" break else: name = raw_input("Try again: ") # Here's the bit I missed out. count += 1 # Of course, we haven't checked the sucker's last guess # so we have to do that now. if count == 2: if name == 'Ben': print "You're right!" else: print "No more tries for you!!!" Hope this helps. Joal G. Need something more straightforward, e.g., a wrapped one-liner: >>> def guess(n=3): print ("You're right!", 'No more tries for you!!!')[n-1 in ...(x for x in xrange(n) for t in [raw_input('Guess my name: ')=='Ben'] ...if not t or iter([]).next())] Okay, now in my opinion, that's just too complex to give to a newbie as a suggested implementation. :) Joal I suppose this would be far too easy to understand, then: pr =['Guess my name', 'Wrong, try again', 'Last chance'] for p in pr: name = raw_input(p+": ") if name == "Ben": print "You're right!" break else: print "Loser: no more tries for you" regards Steve THIS is why I like python! There's always a simple, easy to understand way to do something. If it looks complex, then there must me something wrong. Joal -- http://mail.python.org/mailman/listinfo/python-list
Re: New to programming question
Joal was right. It is a bit beyond me. But I appreciate your response. -- http://mail.python.org/mailman/listinfo/python-list
Re: New to programming question
Thanks for your reply. -- http://mail.python.org/mailman/listinfo/python-list
Re: New to programming question
Thanks for your input. -- http://mail.python.org/mailman/listinfo/python-list
Re: New to programming question
Thanks for your help. It is much appreciated. -- http://mail.python.org/mailman/listinfo/python-list
Re: New to programming question
Joal Heagney wrote: Bengt Richter wrote: On Fri, 01 Apr 2005 07:46:41 GMT, Joal Heagney <[EMAIL PROTECTED]> wrote: Oh goddammmni. I seem to be doing this a lot today. Look below for the extra addition to the code I posted. Joal Heagney wrote: Here's my contribution anycase: count = 0 # Get first input name = raw_input("Guess my name: ") # Give the sucker two extra goes while count < 2: # Check the value of name if name == 'Ben': print "You're right!" break else: name = raw_input("Try again: ") # Here's the bit I missed out. count += 1 # Of course, we haven't checked the sucker's last guess # so we have to do that now. if count == 2: if name == 'Ben': print "You're right!" else: print "No more tries for you!!!" Hope this helps. Joal G. Need something more straightforward, e.g., a wrapped one-liner: >>> def guess(n=3): print ("You're right!", 'No more tries for you!!!')[n-1 in ...(x for x in xrange(n) for t in [raw_input('Guess my name: ')=='Ben'] ...if not t or iter([]).next())] Okay, now in my opinion, that's just too complex to give to a newbie as a suggested implementation. :) Joal I suppose this would be far too easy to understand, then: pr =['Guess my name', 'Wrong, try again', 'Last chance'] for p in pr: name = raw_input(p+": ") if name == "Ben": print "You're right!" break else: print "Loser: no more tries for you" regards Steve -- Steve Holden+1 703 861 4237 +1 800 494 3119 Holden Web LLC http://www.holdenweb.com/ Python Web Programming http://pydish.holdenweb.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: New to programming question
Bengt Richter wrote: On Fri, 01 Apr 2005 07:46:41 GMT, Joal Heagney <[EMAIL PROTECTED]> wrote: Oh goddammmni. I seem to be doing this a lot today. Look below for the extra addition to the code I posted. Joal Heagney wrote: Here's my contribution anycase: count = 0 # Get first input name = raw_input("Guess my name: ") # Give the sucker two extra goes while count < 2: # Check the value of name if name == 'Ben': print "You're right!" break else: name = raw_input("Try again: ") # Here's the bit I missed out. count += 1 # Of course, we haven't checked the sucker's last guess # so we have to do that now. if count == 2: if name == 'Ben': print "You're right!" else: print "No more tries for you!!!" Hope this helps. Joal G. Need something more straightforward, e.g., a wrapped one-liner: >>> def guess(n=3): print ("You're right!", 'No more tries for you!!!')[n-1 in ...(x for x in xrange(n) for t in [raw_input('Guess my name: ')=='Ben'] ...if not t or iter([]).next())] Okay, now in my opinion, that's just too complex to give to a newbie as a suggested implementation. :) Joal -- http://mail.python.org/mailman/listinfo/python-list
Re: New to programming question
On Fri, 01 Apr 2005 07:46:41 GMT, Joal Heagney <[EMAIL PROTECTED]> wrote: >Oh goddammmni. I seem to be doing this a lot today. Look below for >the extra addition to the code I posted. > >Joal Heagney wrote: >> >> Here's my contribution anycase: >> >> count = 0 >> # Get first input >> name = raw_input("Guess my name: ") >> # Give the sucker two extra goes >> while count < 2: >> # Check the value of name >> if name == 'Ben': >> print "You're right!" >> break >> else: >> name = raw_input("Try again: ") > # Here's the bit I missed out. > count += 1 >> # Of course, we haven't checked the sucker's last guess >> # so we have to do that now. >> if count == 2: >> if name == 'Ben': >> print "You're right!" >> else: >> print "No more tries for you!!!" >> >> >> Hope this helps. >> Joal > >G. > Need something more straightforward, e.g., a wrapped one-liner: >>> def guess(n=3): print ("You're right!", 'No more tries for you!!!')[n-1 in ...(x for x in xrange(n) for t in [raw_input('Guess my name: ')=='Ben'] ...if not t or iter([]).next())] ... >>> guess() Guess my name: Jack Guess my name: Bob Guess my name: Ben You're right! >>> guess() Guess my name: Jack Guess my name: Ben You're right! >>> guess() Guess my name: Kermit Guess my name: Ms Piggy Guess my name: Ernie No more tries for you!!! >>> guess(1) Guess my name: Einstein No more tries for you!!! >>> guess() Guess my name: Ben You're right! Regards, Bengt Richter -- http://mail.python.org/mailman/listinfo/python-list
Re: New to programming question
Oh goddammmni. I seem to be doing this a lot today. Look below for the extra addition to the code I posted. Joal Heagney wrote: Here's my contribution anycase: count = 0 # Get first input name = raw_input("Guess my name: ") # Give the sucker two extra goes while count < 2: # Check the value of name if name == 'Ben': print "You're right!" break else: name = raw_input("Try again: ") # Here's the bit I missed out. count += 1 # Of course, we haven't checked the sucker's last guess # so we have to do that now. if count == 2: if name == 'Ben': print "You're right!" else: print "No more tries for you!!!" Hope this helps. Joal G. Joal -- http://mail.python.org/mailman/listinfo/python-list
Re: New to programming question
Ben wrote: This is an exercise from the Non-programmers tutorial for Python by Josh Cogliati. The exercise is: Write a program that has a user guess your name, but they only get 3 chances to do so until the program quits. Here is my script: -- count = 0 name = raw_input("Guess my name. ") while name != 'Ben' and count < 3: Everything inside this loop will only occur if the name doesn't equal 'Ben' and the count is less than 3. count = count + 1 You increase the count by one, which allows your code to catch the case where count = 2 and now equals 3. if name != 'Ben' and count < 3: name = raw_input('Guess again. ') elif name == 'Ben' and count < 3: print "You're right!" else: print 'No more tries.' Which is why you get this print message, because count is now equal to 3. -- Everything works except the line: print "You're right!" But at no point does the program get an opportunity to print "No more tries.' because there is no point inside this loop where name == 'Ben'. Could someone tell me what is wrong and give me a better alternative to what I came up with. Thank you Ben Also, you're duplicating a lot of your case testing. You check to see if the name is 'Ben' at the start, and then inside the loop, and the same for the counts. I tried to write out a logical method of approaching this problem, but in truth this particular use-case isn't that simple is it? Here's my contribution anycase: count = 0 # Get first input name = raw_input("Guess my name: ") # Give the sucker two extra goes while count < 2: # Check the value of name if name == 'Ben': print "You're right!" break else: name = raw_input("Try again: ") # Of course, we haven't checked the sucker's last guess # so we have to do that now. if count == 2: if name == 'Ben': print "You're right!" else: print "No more tries for you!!!" Hope this helps. Joal -- http://mail.python.org/mailman/listinfo/python-list
Re: New to programming question
On 31 Mar 2005 20:03:00 -0800, "Ben" <[EMAIL PROTECTED]> wrote: >Could someone tell me what is wrong and give me a better alternative to >what I came up with. Seperate you raw input statements from your test. Your elsif is skipping over it. Try using only one raw imput statement right after your while statement. Ron -- http://mail.python.org/mailman/listinfo/python-list
Re: New to programming question
Ben wrote: This is an exercise from the Non-programmers tutorial for Python by Josh Cogliati. The exercise is: Write a program that has a user guess your name, but they only get 3 chances to do so until the program quits. Here is my script: -- count = 0 name = raw_input("Guess my name. ") while name != 'Ben' and count < 3: count = count + 1 if name != 'Ben' and count < 3: name = raw_input('Guess again. ') elif name == 'Ben' and count < 3: print "You're right!" else: print 'No more tries.' -- Everything works except the line: print "You're right!" Could someone tell me what is wrong The code within the while loop (i.e., everything indented) is executed only if the while condition is true, i.e., name != Ben and count < 3 So name == 'Ben': will always be false and "You're right!" will never get printed and give me a better alternative to what I came up with. Just a hint: you may find a cleaner solution if you separate the tests for name from the test for count. Thank you Ben HTH Michael -- http://mail.python.org/mailman/listinfo/python-list
New to programming question
This is an exercise from the Non-programmers tutorial for Python by Josh Cogliati. The exercise is: Write a program that has a user guess your name, but they only get 3 chances to do so until the program quits. Here is my script: -- count = 0 name = raw_input("Guess my name. ") while name != 'Ben' and count < 3: count = count + 1 if name != 'Ben' and count < 3: name = raw_input('Guess again. ') elif name == 'Ben' and count < 3: print "You're right!" else: print 'No more tries.' -- Everything works except the line: print "You're right!" Could someone tell me what is wrong and give me a better alternative to what I came up with. Thank you Ben -- http://mail.python.org/mailman/listinfo/python-list