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

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

# Why does it return 0?

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.

• 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)
• 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: