Python Forum
Please check whether the code about the for loop question is correct. (SyntaxError) - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Please check whether the code about the for loop question is correct. (SyntaxError) (/thread-38634.html)

Pages: 1 2


Please check whether the code about the for loop question is correct. (SyntaxError) - lilliancsk01 - Nov-07-2022

print('\n'.join(f'{":".join(str(n)[-1]
for l in range(int(l/2+.5)))}{"" if l%2 else ":"}'
for n, l in (lambda n: ((l, n)
for l in range(n)))(int(input("Enter the size of the pattern:")))))

After I run the above code, a syntax error prompt out: unterminated string literal.
How can I fix the syntax error? Please check whether the code is correct.


Write a program that again generates a square, this time of a different pattern. You should again include code that checks the validity of the input value.

The example shows two different executions of the program. Your program code should be similar to the above program. Printing of digits should have wrap-around feature, i.e. after ‘9’ is ‘0’.

Enter the size of the pattern: 5

0:0:0

1:1:1

2:2:2

3:3:3

4:4:4

Enter the size of the pattern: 14

0:0:0:0:0:0:

1:1:1:1:1:1:

2:2:2:2:2:2:

3:3:3:3:3:3:

4:4:4:4:4:4:

5:5:5:5:5:5:

6:6:6:6:6:6:

7:7:7:7:7:7:

8:8:8:8:8:8:

9:9:9:9:9:9:

0:0:0:0:0:0:

1:1:1:1:1:1:

2:2:2:2:2:2:

3:3:3:3:3:3:


RE: Write a program that generates a square. - deanhystad - Nov-07-2022

Do you have a question?


RE: Write a program that generates a square. - lilliancsk01 - Nov-07-2022

(Nov-07-2022, 03:46 PM)deanhystad Wrote: Do you have a question?
Write loops that generate the given output. Write a program that again generates a square, this time of a different pattern. You should again include code that checks the validity of the input value.


RE: Write loops that generate the given output. - deanhystad - Nov-07-2022

That is not a question. That looks like a homework assignment. If you are having trouble understanding the assignment or writing some piece of python code please ask a specific question. "How do I do this?" will not be accepted.


RE: Write loops that generate the given output. - lilliancsk01 - Nov-07-2022

(Nov-07-2022, 04:36 PM)deanhystad Wrote: That is not a question. That looks like a homework assignment. If you are having trouble understanding the assignment or writing some piece of python code please ask a specific question.
How can I print the numbers with colons using loops?


RE: Write loops that generate the given output. - deanhystad - Nov-07-2022

This being homework, there are rules we both have to follow.

https://python-forum.io/misc.php?action=help&hid=52

Before I can answer your question you need to show what you've tried.

As an aside, I don't see where it says you need to use a for loop for printing the 8:8:8 part.

And I have a question for you. Why isn't the 14 pattern square? It as 14 rows but only 12 columns. I'd expect 14 to look like this:
Output:
0:0:0:0:0:0:0: 1:1:1:1:1:1:1: 2:2:2:2:2:2:2: 3:3:3:3:3:3:3: 4:4:4:4:4:4:4: 5:5:5:5:5:5:5: 6:6:6:6:6:6:6: 7:7:7:7:7:7:7: 8:8:8:8:8:8:8: 9:9:9:9:9:9:9: 0:0:0:0:0:0:0: 1:1:1:1:1:1:1: 2:2:2:2:2:2:2: 3:3:3:3:3:3:3:



RE: Write loops that generate the given output. - lilliancsk01 - Nov-07-2022

(Nov-07-2022, 05:02 PM)deanhystad Wrote: This being homework, there are rules we both have to follow.

https://python-forum.io/misc.php?action=help&hid=52

Before I can answer your question you need to show what you've tried.

As an aside, I don't see where it says you need to use a for loop for printing the 8:8:8 part.

And I have a question for you. Why isn't the 14 pattern square? It as 14 rows but only 12 columns. I'd expect 14 to look like this:
Output:
0:0:0:0:0:0:0: 1:1:1:1:1:1:1: 2:2:2:2:2:2:2: 3:3:3:3:3:3:3: 4:4:4:4:4:4:4: 5:5:5:5:5:5:5: 6:6:6:6:6:6:6: 7:7:7:7:7:7:7: 8:8:8:8:8:8:8: 9:9:9:9:9:9:9: 0:0:0:0:0:0:0: 1:1:1:1:1:1:1: 2:2:2:2:2:2:2: 3:3:3:3:3:3:3:
How to change 10 or 20 to 0? Can you give me some hints to print the colons next to every number?


RE: Write loops that generate the given output. - deanhystad - Nov-07-2022

To convert 13 to 3 there is a math operation you can use, or you could convert the number to a string and only use the last digit. Here's a list of Python math operators.

https://www.w3schools.com/python/python_operators.asp.

I would not print numbers and colons, but if you want to do it that way you can. Here's info about the print command.

https://realpython.com/python-print/

I would make a string of numbers and colons, and print the string. This is a pretty exhaustive list of string operators wiith examples.

https://pythongeeks.org/strings-in-python/


RE: Write loops that generate the given output. - lilliancsk01 - Nov-08-2022

(Nov-07-2022, 05:57 PM)deanhystad Wrote: To convert 13 to 3 there is a math operation you can use, or you could convert the number to a string and only use the last digit. Here's a list of Python math operators.

https://www.w3schools.com/python/python_operators.asp.

I would not print numbers and colons, but if you want to do it that way you can. Here's info about the print command.

https://realpython.com/python-print/

I would make a string of numbers and colons, and print the string. This is a pretty exhaustive list of string operators wiith examples.

https://pythongeeks.org/strings-in-python/

print('\n'.join(f'{":".join(str(n)[-1]
for l in range(int(l/2+.5)))}{"" if l%2 else ":"}'
for n, l in (lambda n: ((l, n)
for l in range(n)))(int(input("Enter the size of the pattern:")))))

After I run the above code, a syntax error prompt out: unterminated string literal.
How can I fix the syntax error? Please check whether the code is correct.


RE: Please check whether the code about the for loop question is correct. (SyntaxError) - deanhystad - Nov-08-2022

Use python tags when posting code. Post entire error message.