Why does it return 0? - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Why does it return 0?

GixugifGixugif Posts: 1Member
[code]def countErrors(secret, lettersGuessed, errors):

if lettersGuessed == "":
errors = errors + 0
else:
subproblem = lettersGuessed[len(lettersGuessed)-1]
others = lettersGuessed[0:len(lettersGuessed)-1]
if subproblem in secret:
errors = errors + 0
else:
errors = errors + 1
print errors, secret, lettersGuessed, others, subproblem
countErrors(secret, others, errors)
return errors




secret = "guinness"
lettersGuessed = "pnitwzg"
errors = 0
x = countErrors(secret, lettersGuessed, errors)
print x
[/code]

Here's what it outputs:

0 guinness pnitwzg pnitwz g
1 guinness pnitwz pnitw z
2 guinness pnitw pnit w
3 guinness pnit pni t
3 guinness pni pn i
3 guinness pn p n
4 guinness p p
0

I'm pretty new at this, but this just doesn't seem to make any sense. It does exactly what I want it to, except instead of returning 4, it returns 0.

Comments

  • bubbatremellbubbatremell Posts: 39Member
    : [code]: def countErrors(secret, lettersGuessed, errors):
    :
    : if lettersGuessed == "":
    : errors = errors + 0
    : else:
    : subproblem = lettersGuessed[len(lettersGuessed)-1]
    : others = lettersGuessed[0:len(lettersGuessed)-1]
    : if subproblem in secret:
    : errors = errors + 0
    : else:
    : errors = errors + 1
    : print errors, secret, lettersGuessed, others, subproblem
    : countErrors(secret, others, errors)
    : return errors
    :
    :
    :
    :
    : secret = "guinness"
    : lettersGuessed = "pnitwzg"
    : errors = 0
    : x = countErrors(secret, lettersGuessed, errors)
    : print x
    : [/code]:
    :
    : Here's what it outputs:
    :
    : 0 guinness pnitwzg pnitwz g
    : 1 guinness pnitwz pnitw z
    : 2 guinness pnitw pnit w
    : 3 guinness pnit pni t
    : 3 guinness pni pn i
    : 3 guinness pn p n
    : 4 guinness p p
    : 0
    :
    : I'm pretty new at this, but this just doesn't seem to make any
    : sense. It does exactly what I want it to, except instead of
    : returning 4, it returns 0.
    :
    :

    Change the second to last line so it says:
    errors = countErrors(secret, others, errors)
  • bubbatremellbubbatremell Posts: 39Member
    : : [code]: : def countErrors(secret, lettersGuessed, errors):
    : :
    : : if lettersGuessed == "":
    : : errors = errors + 0
    : : else:
    : : subproblem = lettersGuessed[len(lettersGuessed)-1]
    : : others = lettersGuessed[0:len(lettersGuessed)-1]
    : : if subproblem in secret:
    : : errors = errors + 0
    : : else:
    : : errors = errors + 1
    : : print errors, secret, lettersGuessed, others, subproblem
    : : countErrors(secret, others, errors)
    : : return errors
    : :
    : :
    : :
    : :
    : : secret = "guinness"
    : : lettersGuessed = "pnitwzg"
    : : errors = 0
    : : x = countErrors(secret, lettersGuessed, errors)
    : : print x
    : : [/code]: :
    : :
    : : Here's what it outputs:
    : :
    : : 0 guinness pnitwzg pnitwz g
    : : 1 guinness pnitwz pnitw z
    : : 2 guinness pnitw pnit w
    : : 3 guinness pnit pni t
    : : 3 guinness pni pn i
    : : 3 guinness pn p n
    : : 4 guinness p p
    : : 0
    : :
    : : I'm pretty new at this, but this just doesn't seem to make any
    : : sense. It does exactly what I want it to, except instead of
    : : returning 4, it returns 0.
    : :
    : :
    :
    : Change the second to last line so it says:
    : errors = countErrors(secret, others, errors)
    :
    Also, you can pretty things up a little if you like:
    errors = errors + 0 -----> pass
    if lettersGuessed == 0: pass / else: --------> if lettersGuessed != 0 (no else needed)
    same thing w/ if subproblem in secret:

Sign In or Register to comment.