New to programming question

B

Ben

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
 
M

Michael Spencer

Ben said:
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
 
R

Ron_Adam

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
 
J

Joal Heagney

Ben said:
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.

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
 
J

Joal Heagney

Oh goddammmnitttt. I seem to be doing this a lot today. Look below for
the extra addition to the code I posted.

Joal said:
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.

Joal
 
B

Bengt Richter

Oh goddammmnitttt. I seem to be doing this a lot today. Look below for
the extra addition to the code I posted.


# Here's the bit I missed out.
count += 1

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())]
... Guess my name: Jack
Guess my name: Bob
Guess my name: Ben
You're right! Guess my name: Jack
Guess my name: Ben
You're right! Guess my name: Kermit
Guess my name: Ms Piggy
Guess my name: Ernie
No more tries for you!!! Guess my name: Einstein
No more tries for you!!! Guess my name: Ben
You're right!

Regards,
Bengt Richter
 
J

Joal Heagney

Bengt said:
Oh goddammmnitttt. I seem to be doing this a lot today. Look below for
the extra addition to the code I posted.



# Here's the bit I missed out.
count += 1


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
 
S

Steve Holden

Joal said:
Bengt said:
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
 
J

Joal Heagney

Steve said:
Joal said:
Bengt said:
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
 
J

Joal Heagney

Joal said:
Steve said:
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:
<do this first>
else:
<now do this last>

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
 
D

Dennis Lee Bieber

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:
said:
Could someone tell me what is wrong and give me a better alternative to
what I came up with.

Thank you
Well, it's been two days since your post, and the other
suggestions should have given you enough to make a working version, so I
shouldn't be violating the "we don't do homework" ethic by too much with
this...

-=-===-=-=-=-=-=-
myName = "Ben"
numTries = 3

print "Guess my name (you get three tries)"
for t in range(numTries):
guess = raw_input("Your Guess> ")
if guess.upper() == myName.upper():
print "You guessed my name in %s tries" % (t + 1)
break
else:
print "%s is not my name" % guess
else:
print "Sorry, you lose"
-=-=-=-=-=-=-=-=-=-

G:\My Documents>python t.py
Guess my name (you get three tries)
Your Guess> Able
Able is not my name
Your Guess> Cain
Cain is not my name
Your Guess> Serpent
Serpent is not my name
Sorry, you lose

G:\My Documents>python t.py
Guess my name (you get three tries)
Your Guess> Who
Who is not my name
Your Guess> is
is is not my name
Your Guess> ben
You guessed my name in 3 tries

G:\My Documents>

--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top