Python question

Asked 2 years ago, Updated 2 years ago, 19 views

while True:
    a+=1
    sum+=a
    if sum>s:
        break

This syntax and

while sum<s:
    a+=1
    sum+=a
    if sum>s:
        break

I wonder why this phrase is different and doesn't work

python

2022-09-20 10:48

1 Answers

Both are running...?

import random
s = random.randomint (0,9987) # I picked randomly just in case there is a problem with the value of s

sum = 0
a = 0
while True:
    a += 1
    sum += a
    if sum > s:
        break
print(sum)

sum = 0
a = 0
while sum < s:
    a += 1
    sum += a
    # If sum > s: # <--- These two lines are actually unnecessary.
    #     This is the case when you think about the principle of operation of break # <--- while.
print(sum)


2022-09-20 10:48

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.