Python Forum
Please check whether the code about the for loop question is correct. (SyntaxError)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Please check whether the code about the for loop question is correct. (SyntaxError)
#1
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:
Reply
#2
Do you have a question?
Reply
#3
(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.
Reply
#4
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.
ndc85430 likes this post
Reply
#5
(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?
Reply
#6
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:
Reply
#7
(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?
Reply
#8
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/
Reply
#9
(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.
Reply
#10
Use python tags when posting code. Post entire error message.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Many iterations for loop question adesimone 9 1,854 Nov-12-2022, 07:08 PM
Last Post: deanhystad
  Beginner Python Question: FIzz Buzz using while loop camoyn13 2 1,809 Sep-20-2022, 09:00 AM
Last Post: deanhystad
  Is that code correct? Sameh 7 2,985 May-22-2021, 04:29 PM
Last Post: Sameh
  Repeat question (for loop) rs74 7 6,503 Jun-17-2020, 03:17 PM
Last Post: rs74
  Question about running comparisons through loop from input value Sunioj 2 2,414 Oct-15-2019, 03:15 PM
Last Post: jefsummers
  Please check code Evgeniy2019 4 3,087 Apr-05-2019, 06:37 PM
Last Post: Evgeniy2019
  SyntaxError: Invalid syntax in a while loop ludegrae 3 14,756 Dec-18-2018, 04:12 PM
Last Post: Larz60+
  Syntax error for code that seems correct mcnhscc39 4 3,599 Nov-15-2018, 08:40 PM
Last Post: j.crater
  while loop question Tripler 4 2,962 Jul-24-2018, 06:37 AM
Last Post: buran
  Loop question kraven 3 3,642 Sep-10-2017, 07:31 AM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020