Python Forum
Can somebody check what is wrong with my code?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Can somebody check what is wrong with my code?
#1
The purpose of the code is to add the digits of the factorial product such as 5! = 120 and the output is 1 + 2 = 3.
import math
num = 10
fac = math.factorial(num)
list = []
n = 0
sum = 0
while fac > 0:
     digit = (fac // 10 ** (n-1)) % 10
     n += 1
     list.append(digit)
     for i in list:
         sum+=i
         break
print(sum)
Reply
#2
Looks to me like you want to create a list of digits by repeatedly dividing over fac. I see a few problems with this.

1. Your while loop test is while fac > 0, but fac is never changed. Even if n is greater than the number of digits, the while test is still successful.

2. Your sum method is not correct. You're appending to a list in the while loop, then before exiting the while loop you're trying to add up all the digits in the list. You need to either add the digits as you encounter them, or you need to add up all the digits in the list after you're done appending to the list.

3. What do you intend the break on line 13 to do? As written, the line 11 for loop will only ever run one iteration.
Reply
#3
EDITED for clarification

Never use built-ins like list and sum as variable names unless you like to live dangerously and recklessly.

Examples of surprises you may encounter:

>>> list('abc')
['a', 'b', 'c']
>>> sum([1, 2, 3])
6
>>> list = []
>>> list('abc')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'list' object is not callable
>>> sum = 0
>>> sum([1, 2, 3])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'int' object is not callable
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#4
It is ok to use list and sum, just not as variable names. I'm sure I'm not the only one to do a double take after reading "Never use built-ins like list and sum unless you like to live dangerously and recklessly."

Your code for getting the digits is very confusing. I think you had an idea and started to code before it was fleshed out. It think you were trying to do this:
    digitsum = 0
    while fact > 0:
        digitsum += fact % 10
        fact //= 10
But you got lost somewhere and forgot to change fact each time through the loop, resulting in a loop that never ends. And because you forgot to change fact you thought you had to divide by increasing powers of 10. I still have no idea why you are using a list to store the digits.
Reply
#5
(Sep-15-2020, 05:06 PM)deanhystad Wrote: It is ok to use list and sum, just not as variable names. I'm sure I'm not the only one to do a double take after reading "Never use built-ins like list and sum unless you like to live dangerously and recklessly."

You are right & my bad. I edited my post to better express what I wanted to say.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I have a code which is very simple but still I cannot detect what's wrong with it max22 1 488 Nov-07-2023, 04:32 PM
Last Post: snippsat
  Something wrong with my code FabianPruitt 5 867 Jul-03-2023, 10:55 PM
Last Post: Pedroski55
  Compiles Python code with no error but giving out no output - what's wrong with it? pythonflea 6 1,575 Mar-27-2023, 07:38 AM
Last Post: buran
  Video recording with Raspberry Pi - What´s wrong with my python code? Montezuma1502 3 1,266 Feb-24-2023, 06:14 PM
Last Post: deanhystad
  Why doesn't this code work? What is wrong with path? Melcu54 7 1,804 Jan-29-2023, 06:24 PM
Last Post: Melcu54
  Am I wrong or is Udemy wrong? String Slicing! Mavoz 3 2,572 Nov-05-2022, 11:33 AM
Last Post: Mavoz
  Code to check folder and sub folders for new file and alert fioranosnake 2 1,952 Jan-06-2022, 05:03 PM
Last Post: deanhystad
  Wrong code in Python exercise MaartenRo 2 1,535 Jan-01-2022, 04:12 PM
Last Post: MaartenRo
  The code I have written removes the desired number of rows, but wrong rows Jdesi1983 0 1,636 Dec-08-2021, 04:42 AM
Last Post: Jdesi1983
  Try,Except,Else to check that user has entered either y or n (Code block pasted) RandomNameGenerator 3 2,343 Jun-29-2021, 08:21 PM
Last Post: RandomNameGenerator

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020