英文:
I am writing a program that generates a list of integer coordinates for all points in the first quadrant from (1, 1) to (5, 5)
问题
I have to use list comprehension which I don't know how to use because I have just started learning Python. It displays the error message:
TypeError: 'int' object is not iterable
So this is the code I have currently written:
frst_quadrant = 1
for a in frst_quadrant:
a =+ 1
if a > 5:
break
elif a < 5:
a =+ 1
print(a)
continue
else:
a =+ 1
print(a)
continue
Please help me solve this problem.
英文:
And I have to use list comprehension which i dont how to use because i have just started learning python
It displays the error message
TypeError: 'int' object is not iterable
so this is the code i have currently written
frst_quadrant = 1
for a in frst_quadrant:
a =+ 1
if a > 5:
break
elif a < 5:
a =+ 1
print(a)
continue
else:
a =+ 1
print(a)
continue
Please help me solve this problem
答案1
得分: 0
第一行中,你将first_quadrant写成了一个整数1。第二行中,你尝试在一个整数上使用for循环。这是不允许的,因此会出现错误。
你可以用很多其他方法来做这个。我写了两种可能的解决方案。
解决方案1
first_quadrant = 1
last_quadrant = 5
coordinates = []
for x in range(first_quadrant, last_quadrant + 1, 1):
for y in range(first_quadrant, last_quadrant + 1, 1):
coordinates.append([x, y])
print(coordinates)
解决方案2
coordinates = [[x, y] for x in range(1, 5 + 1, 1) for y in range(1, 5 + 1, 1)]
print(coordinates)
英文:
In the first line you have written first_quadrant as 1 which is an integer. In the second line you are trying to use a for loop over an integer. This is not allowed and therefore the error.
You could do it in many other ways. I am writing two such possible solutions.
Solution 1
first_quadrant = 1
last_quadrant = 5
coordinates = []
for x in range(first_quadrant,last_quadrant +1, 1):
for y in range(first_quadrant,last_quadrant +1, 1):
coordinates.append([x,y])
print(coordinates)
Solution 2
coordinates = [[x,y)] for x in range(1, 5+1,1) for y in range(1, 5+1, 1)]
print(coordinates)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论