While loop to add numbers in list python

I am learning python V3 and have the following problem on a homework assignment:

In this exercise, your function will receive a list of numbers, and an integer. It will add the values in the list to a total as long as the total is less than or equal to the value of the second parameter. The sum of numbers in the list will always be larger than the second parameter's value.

I came up with the following solution:

def while11[nums,maxi]:
    i=0
    total=0
    while total 0:
    sum += num
    num -= 1
print["The sum is", sum]

Output:

User input number sum

sum = 0

num = int[input["Enter a number: "]]
if num < 0:
    print["Please enter a positive number"]
else:
    sum = 0

# use while loop to iterate until zero
while num > 0:
    sum += num
    num -= 1
print["The sum is", sum]

Output:

Enter a number: 10
The sum is 55

Do comment if you have any doubts or suggestions on this Python sum topic.

Note: IDE: PyCharm 2021.3.3 [Community Edition]

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Degree in Computer Science and Engineer: App Developer and has multiple Programming languages experience. Enthusiasm for technology & like learning technical.

General Knowledge

Learn Popular Language

Blogs

  • Jan 3

    Stateful vs Stateless

    A Stateful application recalls explicit subtleties of a client like profile, inclinations, and client activities...

  • Dec 29

    Best programming language to learn in 2021

    In this article, we have mentioned the analyzed results of the best programming language for 2021...

  • Dec 20

    How is Python best for mobile app development?

    Python has a set of useful Libraries and Packages that minimize the use of code...

  • July 18

    Learn all about Emoji

    In this article, we have mentioned all about emojis. It's invention, world emoji day, emojicode programming language and much more...

  • Jan 10

    Data Science Recruitment of Freshers

    In this article, we have mentioned about the recruitment of data science. Data Science is a buzz for every technician...

Follow us


Can you sum numbers in a while loop in Python?

While loop to calculate sum and average Run a while loop till n is greater than zero. In each iteration, add the current value of n to the sum variable and decrement n by 1. Calculates the average by dividing the sum by n [total numbers].

How do you add a number to a while loop?

Sum of Natural Numbers Using while Loop In both programs, the loop is iterated n number of times. And, in each iteration, the value of i is added to sum and i is incremented by 1 . Though both programs are technically correct, it is better to use for loop in this case. It's because the number of iterations is known.

How do you add numbers in a loop in Python?

“how to add numbers in python using for loop” Code Answer's.
n = input["Enter Number to calculate sum"].
n = int [n].
sum = 0..
for num in range[0, n+1, 1]:.
sum = sum+num..
print["SUM of first ", n, "numbers is: ", sum ].

Can you add numbers in a list Python?

sum[] function in Python Python provides an inbuilt function sum[] which sums up the numbers in the list.

Bài Viết Liên Quan

Toplist mới

Bài mới nhất

Chủ Đề