Synthetic code questions from Python novice 1 to n

Asked 1 weeks ago, Updated 1 weeks ago, 1 views

num=int (input ("Enter a number that you want to add up")
i=1
sum=0

while i <= num:
    sum=sum+i
    i=i+1

print(the sum of "1" to "num" is :",sum)

I'd like you to tell me why you specify the i, sum variable and why you initialize i=1,sum=0!

python

2022-09-20 08:56

1 Answers

How many more should I add? = 17176
Sum = (How many more should I add?) * (How many more should I add?) + 1)) / 2
print("From 1" to what number shall we add, the sum from " is :", int(sum))

'''
If you know the ordinal sequence formula, code it as above.

If you don't know the ordinal sequence formula, code it as follows.
Let's add one by one.
To do that, you have to keep pointing out "how much I've added now" and "how much is the sum so far."
Now think about what i and sum correspond to, for yourself!
'''

How many more = 17176
More = 1
Sum = 0
while plus <= how many more shall we add:
    Total = Total + Addition
    Additive number = Additive number + 1
print("From 1 to what number shall we add, sum to :", sum)


2022-09-20 08:56

If you have any answers or tips


© 2022 pinfo. All rights reserved.