[Tutor] Not sure why the code is giving weird result?

Steven D'Aprano steve at pearwood.info
Sat Jan 9 09:20:21 EST 2016


On Sat, Jan 09, 2016 at 11:56:09AM +1000, Whom Isac wrote:

"Not sure why the code is giving weird result?"

Neither are we. What do you mean "weird results"? Which code did you 
run? (You give two versions.) What result did you expect, and what 
result did you get?


But I can see one horrible mistake in your code:

>        try:
>            [code]
>        except Exception:
>            print("Sorry About that")

That's the WORST possible thing you can do as a programmer. That's like 
poking your eyes out with sharp stick, and then complaining that you 
can't see what it going on. Of course you can't see what is going on, 
you've blinded yourself.

Instead of Python showing you the error, you get a silly apology "Sorry 
about that". Sorry about what? Nobody knows! Why did it happen? It is 
impossible to say!

Step 1: 

Get rid of the "try...except" block. try...except is very useful, but 
here it is just hurting you. It is making it impossible for you to debug 
your code, since you can't see where the bugs are.



> def factorial():
>     print("Here you will put your factorial")
>     factVar=int(input("Please input what number to be factorial: \t"))
>     TValue=0
>     for i in range(0,factVar+1):
>         TValue+=i*i+1
>     print("Your total factorial: "+str(TValue))

I don't think that calculates what you think it calculates. If you enter 
5, the calculation will be:

TValue = 0
Add 0*0 + 1 = 1 gives TValue = 1
Add 1*1 + 1 = 2 gives TValue = 3
Add 2*2 + 1 = 5 gives TValue = 8
Add 3*3 + 1 = 10 gives TValue = 18
Add 4*4 + 1 = 17 gives TValue = 35
Add 5*5 + 1 = 26 gives TValue = 61

which is a little bit more than half the correct value, 5! = 120.

Try this instead:

from math import factorial
print(factorial(5))


I don't understand the rest of your code. In your own words, step by 
step as if you were writing out a recipe, what is it supposed to be 
doing?


>     value=TValue
>     dvdVal=1
>     divider=1
>     totalTrailingZeroes=0
> 
>     answer=int(value/5**(divider))
>     totalTrailingZeroes+=answer
>     newanswer=round(answer,1)
>     while newanswer !=0:
>         if newanswer >0:
>             answer=int(value/(5**(divider+1)))
>             newanswer=round(answer,1)
>             totalTrailingZeroes+=newanswer
>             print(str(totalTrailingZeroes))
>         else:
>             newanswer=0
>             print(str(TrailingZeroes))


By the way, you don't need any of those calls to "round()". They don't 
do anything. Since "answer" is already an int, you call round on an int, 
which rounds it to give exactly the same number:

py> answer = 42
py> newanswer = round(answer, 1)
py> newanswer
42

So you can save time and effort by just saying:

newanswer = answer

But why bother? What's the purpose of having two variables "answer" and 
"newanswer"?



-- 
Steve


More information about the Tutor mailing list