Overview
Another programming assignment that I found on r/learnpython
.
Looks like the OP needs help with a Python assignment. There hasn’t
been much traction on it. It’s about two months old. So, it should
be fair game! Let’s go!
Assignment
Below is the assignment.
hey guys! I need help with this: how do you use python to print a program that prints out a list from 1-6,4 times and have one gap between each list and it is vertical.
Below is their desired output based on the OP’s comment. Where is the gap between each list? Though, this is not what they explained. So, I am going to do both. The requirements from their explaination. Then, I will do it from the expected output.
1
2
3
4
5
6
1
2
3
4
5
6
1
2
3
4
5
6
1
2
3
4
5
6
Solutions
Solution 1 - As Explained
This is the solution to the problem the OP provided. As explained.
- It lists 1-6 four times.
- It has one gap between each list.
- It’s vertical.
Code
Using a for()
loop to iterate between the numbers one through
four using range()
. Nested another for()
loop within it and
iterated though one and seven. Printed the number it’s on. Printed an
empty line after the nested iteration was completed.
This continues until the range of the parent loop reaches the the end. In this case it’s four. So, when it hits five. The operation stops.
This can be done withwhile()
loops too. I’ll update this post
later.
"""
file: python.py
author: Timothy (n3s0)
description:
The solution that I came up with for this.
hey guys! I need help with this: how do you use python to print a program that prints
out a list from 1-6,4 times and have one gap between each list and it is vertical.
"""
# Iterate through the range 1 through 4 and assign each
# value to the variable counter.
for counter in range(1, 5):
# iterate through 1 and 6 and assign each value to the
# the variable number
for number in range(1, 7):
# print the number
print(number)
# print the gap requested.
print()
Output
Output can be seen below and it looks like we have the desired outcome.
python homework.py
1
2
3
4
5
6
1
2
3
4
5
6
1
2
3
4
5
6
1
2
3
4
5
6
Solution 2 - Expected Output
This provides the solution for the output they were expecting.
Code
It’s the same code. Only it doesn’t print a new line after the six.
"""
file: homework.py
author: Timothy (n3s0)
description:
This is the solution that I came up with.
hey guys! I need help with this: how do you use python to print a program that prints
out a list from 1-6,4 times and have one gap between each list and it is vertical.
"""
# Iterate through the range 1 through 4 and assign each
# value to the variable counter.
for counter in range(1, 5):
# iterate through 1 and 6 and assign each value to the
# the variable number
for number in range(1, 7):
# print the number
print(number)
Output
python homework.py
1
2
3
4
5
6
1
2
3
4
5
6
1
2
3
4
5
6
1
2
3
4
5
6
Resources
Providing the resources that helped with this below.