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

Peter Otten __peter__ at web.de
Sat Jan 9 05:03:40 EST 2016


Whom Isac wrote:

> Hi, today I tried to help with one of the request in Python tutor about
> trailing zeros -6days old.
> I don't know why my code is incrementing in number.
> Here is the code:
> 
> 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

That's not the "factorial". Look up its definition, change the loop 
accordingly, and if the result still isn't correct put 

          print(TValue)

in the for loop to ensure that it calculates what you think it does.

>           answer=int(value/5**(divider))

Just a general remark as I haven't checked the complete function. Python has 
an integer division operator that allows to write this

answer = value // 5**divider

This gives the correct answer where the use of float in intermediate values 
introduces errors. Compare:

>>> int(10**24 / 5)
199999999999999983222784
>>> 10**24 // 5
200000000000000000000000




More information about the Tutor mailing list