Why the code shows all the addition process?

2024/11/16 11:37:12

Code:

sum=0
for i in range(10,91):sum=sum+iprint(sum)

When I wrote this code, the answer was Output:

10
21
33
46
60
75
91
108
126
145
165
186
208
231
255
280
306
333
361
390
420
451
483
516
550
585
621
658
696
735
775
816
858
901
945
990
1036
1083
1131
1180
1230
1281
1333
1386
1440
1495
1551
1608
1666
1725
1785
1846
1908
1971
2035
2100
2166
2233
2301
2370
2440
2511
2583
2656
2730
2805
2881
2958
3036
3115
3195
3276
3358
3441
3525
3610
3696
3783
3871
3960
4050

What is the problem? Please help me

Answer

This is what your code should look like if you do not want to see each calculation.

sum = 0
for i in range(10,91):sum = sum + i
print(sum)

On a sidenote, as suggested by @Albin Paul, sum is a builtin function, therefore it is recommended to avoid using it as a variable name, since it overwrites the function definition.

https://en.xdnf.cn/q/118934.html

Related Q&A

Creating a list of keywords by scrolling through a dataframe (python)

I have a dataframe that looks like this: dataFrame = pd.DataFrame({Name: ((" Verbundmrtel , Compound Mortar , Malta per stucchi e per incollaggio "),(" StoLevell In Absolute , StoLeve…

How to click this button with python selenium

Im looking to click the button highlighted in the screenshot below; have tried with pyautogui but found results to be inconsistent so trying selenium instead.Im having trouble identifying the button to…

How to find the average of numbers being input, with 0 breaking the loop?

I just need to figure out how to find the average of all these input numbers by the user while using 0 as a exit of the loop. I need to figure out how to eliminate using 0 as part of the average. examp…

NoSuchElementException when loading code using Selenium on Heroku

Error: ERROR:asyncio:Task exception was never retrieved2022-03-14T14:08:52.425684+00:00 app[worker.1]: future: <Task finished name=Task-30 coro=<Dispatcher._process_polling_updates() done, define…

Python alphanumeric

Problem:I have to go through text file that has lines of strings and determine about each line if it is alphanumeric or not. If the line is alphanumeric print for example "5345m34534l is alphanume…

Python 3 - exec() Vs eval() - Expression evaluation [duplicate]

This question already has answers here:Whats the difference between eval, exec, and compile?(3 answers)Closed 7 years ago.After reading query.below python code is still not clear,>>> exec(pri…

What is the syntax for printing multiple data types in Python? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 8 years ago.Improve…

When scraping all the div to get the data getting the null list using lxml in python

I want to scrape the product title , product link , product price but when I am using the xpath it is showing the null list . How to add the xpath and for loop to get the above details . I have tried …

Python how to convert this for loop into a while loop [duplicate]

This question already has answers here:Closed 11 years ago.Possible Duplicate:Converting a for loop to a while loop I have this for a for loop which I made I was wondering how I would write so it woul…

Joining elements in Python list

Given a string, say s=135 and a list, say A=[1,2,3,4,5,6,7], how can I separate the values in the list that are also in s (a digit of s) from the other elements and concatenate these other elements. Th…