New to programming question

Joal Heagney joal at bigpond.net.au
Fri Apr 1 22:56:34 EST 2005


Steve Holden wrote:
> Joal Heagney wrote:
> 
>> Bengt Richter wrote:
>>
>>> On Fri, 01 Apr 2005 07:46:41 GMT, Joal Heagney <joal at bigpond.net.au> 
>>> wrote:
>>>
>>>
>>>> Oh goddammmnitttt. 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
>>>>
>>>>
>>>>
>>>> GRRRRRRRR.
>>>>
>>>
>>>
>>> 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



More information about the Python-list mailing list