New to programming question
Michael Spencer
mahs at telcopartners.com
Thu Mar 31 23:17:24 EST 2005
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
More information about the Python-list
mailing list