Four things:

  1. Not sure why you storing the input as float, int should suffice.
  2. If you give a negative input, it will enter the while loop. However, in the while loop, you are not actually assigning the new input to user_input. Fix this by adding user_input =
  3. The while loop guarantees user_input is >= 0, so if user_input > 0: is unnecessary.
  4. Probably the most important, to calculate the sum of digits, you need to repeatedly divide and sum, not just do it once. So, add a while loop.

Final code:

user_input = int(input("Please Enter Your Number: "))
s = 0

while user_input < 0:
    user_input = int(input("Please Enter Another Number: "))

while user_input:
    s += user_input % 10
    user_input //= 10

print("You've entered a positive number! The sum of the digits is: ", s)
Answer from iz_ on Stack Overflow
🌐
Real Python
realpython.com › python-sum-function
Python's sum(): The Pythonic Way to Sum Values – Real Python
March 18, 2026 - In situations like these, whether you have a long or short list of numbers, Python can be quite useful to solve summation problems. If you want to sum the numbers by creating your own solution from scratch, then you can try using a for loop: ... >>> numbers = [1, 2, 3, 4, 5] >>> total = 0 >>> for number in numbers: ... total += number ... >>> total 15 · Here, you first create total and initialize it to 0. This variable works as an accumulator in which you store intermediate results until you get the final one.
🌐
Career Karma
careerkarma.com › blog › python › python sum: a step-by-step guide
Python Sum: A Step-By-Step Guide | Career Karma
December 1, 2023 - You could use the sum() function to perform this calculation. In Python code, the sum() function can be used to calculate the sum of all values in an iterable object.
Discussions

Finding Sum of Variables in Python - Stack Overflow
Additionally, you're loop to re-enter ... the new value to the variable. And you probably also want to outdent the print statement lest it be done on every iteration of the loop you're about to add. Of course, some may suggest a more Pythonic way of summing the digits of a positive ... More on stackoverflow.com
🌐 stackoverflow.com
How would I make a variable that has the sum of two other variables in python?
But you had it right, you can add two variables together with the + symbol. Also, a side note, when you print something, make sure whatever you want printed is in parentheses directly after print, otherwise python won't understand what you're telling it to do. More on teamtreehouse.com
🌐 teamtreehouse.com
2
October 17, 2017
python - Adding multiple variables to get the sum? - Stack Overflow
Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... Save this question. Show activity on this post. I'm working on a small project regarding a game using Python and Discord webhooks. I'm wondering how to get the total ( sum ) of the variables that ... More on stackoverflow.com
🌐 stackoverflow.com
How do you make two variables add together in Python? - Stack Overflow
By default, input returns the user entered data as string. So, when you use + operator on two strings, it concatenates them. You need to explicitly convert them to int like this · variable1 = int(input()) ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Quora
quora.com › How-do-we-find-the-sum-of-two-variables-in-the-Python-programming-language
How do we find the sum of two variables in the Python programming language? - Quora
Answer (1 of 2): To find the sum of two variables in Python, you can use the [code ]+[/code] operator. For example: [code]>>> x = 3 >>> y = 4 >>> z = x + y >>> print(z) 7 [/code]You can also use the [code ]sum()[/code] function from the [code ]math[/code] module: [code]Copy code>>> import math ...
Top answer
1 of 3
1

Four things:

  1. Not sure why you storing the input as float, int should suffice.
  2. If you give a negative input, it will enter the while loop. However, in the while loop, you are not actually assigning the new input to user_input. Fix this by adding user_input =
  3. The while loop guarantees user_input is >= 0, so if user_input > 0: is unnecessary.
  4. Probably the most important, to calculate the sum of digits, you need to repeatedly divide and sum, not just do it once. So, add a while loop.

Final code:

user_input = int(input("Please Enter Your Number: "))
s = 0

while user_input < 0:
    user_input = int(input("Please Enter Another Number: "))

while user_input:
    s += user_input % 10
    user_input //= 10

print("You've entered a positive number! The sum of the digits is: ", s)
2 of 3
0

The if statement is generally used to decide if something should be done once.

If you want to keep going until user_input becomes zero, you'll need a while.

Also, I'm not entirely certain why you're storing the number as a float, especially when you make that from an int anyway. It may as well just be an int.

Additionally, you're loop to re-enter the value if it was negative doesn't actually assign the new value to the variable.

And you probably also want to outdent the print statement lest it be done on every iteration of the loop you're about to add.


Of course, some may suggest a more Pythonic way of summing the digits of a positive number is a simple:

sum([int(ch) for ch in str(x)])

That works just as well, without having to worry about explicit loops.

🌐
W3Schools
w3schools.com › python › ref_func_sum.asp
Python sum() Function
The sum() function returns a number, the sum of all items in an iterable. ... Coding fundamentals as bite-sized lessons and challenges. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: ...
🌐
Python Guides
pythonguides.com › add-two-variables-in-python
How To Add Two Variables In Python?
November 24, 2025 - The sum() function in Python is used to sum up the elements of an iterable (e.g., list, tuple). It can also be used to add two variables by passing them as elements of a list or tuple.
Find elsewhere
🌐
The New Stack
thenewstack.io › home › what is python’s sum() function and how do you use it?
What Is Python's sum() Function and How Do You Use It? - The New Stack
September 5, 2024 - And given that adding several numbers together is a widely used step in computations, it’s pretty handy that Python can do this. You could even use a for loop to sum a string of numbers together like this: a = [3, 5, 7, 10] total = 0 for number in a: total += number print(total) The above would output the same value as the two-line code above it. In the above code, total is initialized to 0, and then the variable acts as an accumulator as it iterates through the loop.
🌐
University of Vermont
uvm.edu › ~cbcafier › cs1210 › book › 11_loops › loops_and_summation.html
Math and Python: loops and summation – Clayton Cafiero
Here’s a simple example. t = ... values, t. Since the elements of t are all numeric, we can calculate their sum. First, we create a variable to hold the result of the sum....
🌐
GeeksforGeeks
geeksforgeeks.org › python › sum-function-python
sum() function in Python - GeeksforGeeks
November 27, 2025 - The sum() function in Python is used to add up numbers from any iterable such as a list, tuple, set, or dictionary values.
🌐
Codecademy
codecademy.com › forum_questions › 53bb48ee7c82ca502900014b
Can someone explain how to use the sum() function for more than one variable? | Codecademy
use + for this sum is used to sum up all items of a collection such as a list in this case you don’t have a list of numbers, you have 3 variables, it would be silly to put them in a list just to be able to use sum when + is readily available
🌐
Programiz
programiz.com › python-programming › methods › built-in › sum
Python sum()
numbers_sum = sum(numbers) print(numbers_sum) # start = 10 ... If you need to add floating-point numbers with exact precision, then you should use math.fsum(iterable) instead.
🌐
Toppr
toppr.com › guides › python-guide › references › methods-and-functions › methods › sum › built-in › python-sum
Python sum(): Sum() Parameters, Return Value from sum()
July 30, 2021 - The sum () function is used to add the numerical values, whereas the join () function concatenates the items of the iterables. These items are in the form of strings or characters. ... The sum () function in Python is available in the function directory that provides the sum of all the numerical ...
🌐
Python.org
discuss.python.org › python help
Trying to compute sum of list variables over a certain number - Python Help - Discussions on Python.org
May 1, 2024 - I’m on Coursera trying to do this one practice problem and it’s blocking my progress. I’ve analyzed all my past training materials to see if I should know the solution already. no luck! I don’t really want anyone to spit out an instant answer at me, but I don’t know how to beat this.
🌐
YouTube
youtube.com › watch
Use of Sum() In Python - Sum() Function Explained For Summing Values - YouTube
Use of Sum Function In Python - Summing ValuesI did my best to explain use of sum() function in python in easy steps for beginners :)#learnpython #pythonforb...
Published: December 6, 2024
🌐
YouTube
youtube.com › real python
Using Python's Built-in sum() Function - YouTube
Adding several numbers together is a common intermediate step in many computations, so sum() is a pretty handy tool for a Python programmer. Python’s built-i...
Published: March 23, 2023
Views: 2K