Sum from 1 to n, 2 to n, ... n in python

2024/7/8 6:15:06

I was trying to get a series of sum from 1 to n, 2 to n, ..., and n

For example, if n=5, then the result should be 15 14 12 9 5

Please comment for the code below. I can't figure out what's wrong.

n=int(input())
sum=0
m=0
factorial=1for i in range(1, n + 1):factorial *= isum=factorial-mprint(sum)
Answer

One reasonably simple approach:

n = 5
s = sum(range(n+1))
for i in range(n):s -= iprint(s)15
14
12
9
5
https://en.xdnf.cn/q/120412.html

Related Q&A

How do I scrape the about section of a Facebook page? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 6…

How can I dynamically create a name of a variable in Python? [duplicate]

This question already has answers here:How do I create variable variables?(18 answers)Closed 6 years ago.I am having a function in pythondef func(dataFrame,country,sex):varible_name=dataFrame[(dataFra…

Why are Python dictionaries NOT stored in the order they were created? [duplicate]

This question already has answers here:Why is the order in dictionaries and sets arbitrary?(5 answers)Closed 8 years ago.Just curious more than anything else, but why isnt a dictionary such as the one…

How to extract out a non-continuous matrix from a larger matrix based on a list [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 2…

Python get info from long complicated string

Im trying to get info from strings that I have parsed. Im trying to get the font size. This is the string Im returning style="fill: rgb(0, 0, 0); fill-opacity: 1; font-family: ProjectStocksFont; f…

How can i ask for user input and use that input for this text summarizer i created? [duplicate]

This question already has answers here:How do I read from stdin?(25 answers)Closed last year.I have created a text summarizer based on a code I found on Github. Im trying to have to script ask for the…

Return to main menu function for python calculator program

I was asked to write a calculator in Python, and I completed it, but there is only one issue that needs to be fixed. What is the prerequisite? "Once the user inputs/selects an arithmetic operatio…

what the code to use in # Complete this function with recursion in lines 4

# TODO: 2. Create a function that counts the sum of all the numbers in a list belownumber = [1,2,3,4,5] # Use this list as inputdef hitung_total(listKu):# Complete this function with recursionreturn li…

Pip cannot install some Python packages due to missing platform tag in M1 Mac

Lately I have been facing issues installing packages through pip. For e.g. when I try to install the torch package, it fails with the below error > arch arm64 > python --version 3.10.12 ❯ pip --…

find (i,j) location of closest (long,lat) values in a 2D array [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 9…