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.
- Home
- PHP
- MySQL
- MongoDB
- HTML
- Javascript
- Node.js
- Express.js
- Python
- Jquery
- R
- Kotlin
- DS
- Blogs
- Theory of Computation
General Knowledge
Learn Popular Language
A Stateful application recalls explicit subtleties of a client like profile, inclinations, and client activities... In this article, we have mentioned the analyzed results of the best programming language for 2021... Python has a set of useful Libraries and Packages that minimize the use of code... In this article, we have mentioned all about emojis. It's invention, world emoji day, emojicode programming language and much more... In this
article, we have mentioned about the recruitment of data science. Data Science is a buzz for every technician...Blogs
Stateful vs Stateless
Best programming language to learn in 2021
How is Python best for mobile app development?
Learn all about Emoji
Data Science Recruitment of Freshers
Follow us
- eTutorialsPoint©Copyright 2016-2022. All Rights Reserved.