Issue
How can I print numerical interval from -20 to 20 using loop "while" and then print 6 first elements
I tried this to print numbers from -20 to 20 but still don't know how to print first six numbers.
x = -21
while x < 20:
x = x + 1
print(x)
Solution
Assign count = 6
, decrease count
after increasing and printing x
, then use break
if
count
is 0 or False
:
x = -21
count = 6
while x < 20:
if not count: break
x += 1
count -= 1
print(x)
If you don't want to use if
and break
:
x = -21
count = 6
while x < 20 and count:
x += 1
count -= 1
print(x)
Or using count = 0
and increase it until 6:
x = -21
count = 0
while x < 20 and count < 6:
x += 1
count += 1
print(x)
Answered By - Arifa Chan
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.