Hi was just playing around and saw that these 2 codes result in two different outcomes. Obviously the outcomes going to be different but why? How do they work differently? Thanks in advance!
1st code
number = [5,2,5,2,2]
for x_count in number:
output = ""
for count in range(x_count):
output = output + "x"
print(output)
2nd code
number = [5,2,5,2,2]
for x_count in number:
output = ""
for count in range(x_count):
output = output + "x"
print(output)
The range()
function returns a sequence of numbers, starting from 0 and increments by 1, and stops BEFORE a specified number.
You just need to walk down the loops in steps to figure out what's happening:
You have a list of integers of 5 and 2. with a total of 5 items.
number = [5,2,5,2,2]
In the first outer loop, the x_count variable holds the value of each item in the list
for x_count in number:
That means:
x_count = 5
x_count = 2
x_count = 5
...and so onNext, you're creating a variable output
to hold a string of characters:
output = ""
The inner loop is where it gets tricky:
for count in range(x_count):
the range function would generate a range of values from zero to x_count
, remember the value of the x_count
variable is dependent on the current iteration of the outer loop. so the range function argument would look like this:
range(5)
range(2)
range(5)
...and so onWithin the inner loop, you're concatenating the string "x" to the existing value of output
. And ONLY WHEN the inner loop is complete, you print the value of output
(because the print statement is outside the inner loop ie within the outer loop context). That means:
output
is "xxxxx"
because the inner loop counts up to 5 ( range(x_count) where x_count = 5
)output
is "xx"
because the inner loop counts up to 2 ( range(x_count) where x_count = 2
)...and so on.
Printing the value of range outside the inner loop would only give you 5 strings, because the print statement is within the outer loop and the outer loop only counts up to 5 (length of the number
list).
Everything in the first program applies except the context of the print statement. in this case, the print
statement is within the inner loop context which means it only prints values when an iteration happens within the context of the inner loop. it will print the value of output
in this order:
x_count = 5
therefore on the first iteration of the inner loop, the print statement will be called 5 times because the loop counts from 0 - 5 ( range(5)
),
output
.Result:
x
xx
xxx
xxxx
xxxxx
x_count = 2
, therefore on the second iteration of the inner loop, the print statement will execute 2 times because the loop counts from 0 - 2 ( range(2)
)
output
Result:
x
xx
Repeating this flow for the next 3 items in the list. you get:
range(5):
x
xx
xxx
xxxx
xxxxx
range(2):
x
xx
range(2):
x
xx
Putting it all together when both loops are complete, you have this: Result:
x
xx
xxx
xxxx
xxxxx
x
xx
x
xx
xxx
xxxx
xxxxx
x
xx
x
xx
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.