英文:
Print function printing out of order
问题
运行在VSCode中的Python 3.11.2。我有一个简单的函数,但无论出于什么原因,它首先打印fString中的最后一个项目,然后在不同的行上打印fString的其余部分。应该打印最后一个项目的地方,实际上打印出None。
filtered_sortclasses
列表包含了一堆由我创建的 SortClass
类创建的Python对象,我现在将发布那段代码。
class SortClass:
def __init__(self, item):
self.common_name = item.name
self.tier = item.tier
self.min_price = item.price
self.items = [item]
实际上,在以下代码中调用了 SortClass
类。请注意,auctions
列表中的每个 item
都是它自己的类,这应该与问题无关。如果需要发布 Itemize
类的代码,我可以这样做。
sortclasses = []
auctions = self.auctions
while auctions:
item = auctions.pop(0)
matched = False
for sc in sortclasses:
if sc.common_name == item.name and sc.tier == item.tier:
matched = True
if item.price < sc.min_price:
sc.min_price = item.price
sc.items.append(item)
break
if not matched:
sortclasses.append(SortClass(item))
最后,上述打印错误发生在以下代码中。请注意,通过筛选过程,一些 SortClass
对象已被删除,但这些类本身没有以任何方式更改。
for sc in filtered_sortclasses:
print(f'{sc.common_name}, {sc.tier}, {sc.min_price}, {print(sc.items)}')
input('Continue')
以下是一个示例输出:
[<__main__.Itemize object at 0x0000022490C3F390>,... <42 items hidden for your convenience>, <__main__.Itemize object at 0x000002248892B110>]
Auto Recombobulator, LEGENDARY, 10800000, None
再次强调,项目列表,也就是 class
对象,应该出现在第二行输出的末尾的 NoneType
的位置,但实际上首先被打印出来,而且与其他所有打印的内容在不同的输出行上。当我尝试用 {print(sc.items)}
替换为 {print(len(sc.items))}
时,也会发生这种情况。有关可能导致这种情况的想法吗?
英文:
Running python 3.11.2 in VSCode. I've got a simple function, and for whatever reason, it prints the last item in the fString first, on a different line, then prints the rest of the fString. Where the last item should be printed, None is printed instead.
The filtered_sortclasses
list contains a bunch of python objects created by the SortClass
class that I've made, I'll post that code now.
class SortClass:
def __init__(self, item):
self.common_name = item.name
self.tier = item.tier
self.min_price = item.price
self.items = [item]
The SortClass
class is actually called in the following code. Note that each item
in the auctions
list is its own class, which should not be relevant. If I need to post the code for the Itemize
class, I can do so.
sortclasses = []
auctions = self.auctions
while auctions:
item = auctions.pop(0)
matched = False
for sc in sortclasses:
if sc.common_name == item.name and sc.tier == item.tier:
matched = True
if item.price < sc.min_price:
sc.min_price = item.price
sc.items.append(item)
break
if not matched:
sortclasses.append(SortClass(item))
Finally, the above print error occurs in the following code. Note that some of the SortClass
objects were removed through a filtering process, but the classes themselves were not changed in any way.
for sc in filtered_sortclasses:
print(f'{sc.common_name}, {sc.tier}, {sc.min_price}, {print(sc.items)}')
input('Continue')
Here's an example output:
[<__main__.Itemize object at 0x0000022490C3F390>,... <42 items hidden for your convenience>, <__main__.Itemize object at 0x000002248892B110>]
Auto Recombobulator, LEGENDARY, 10800000, None
Again, the list of items, the class
objects, should be in the place of the NoneType
at the end of the second output line, but instead get printed first, and are on a separate output line from everything else that is printed. This also happened when I tried replacing {print(sc.items)}
with {print(len(sc.items))}
. Any ideas on what could be causing this?
答案1
得分: 2
Your issue is right here in the print statement:
for sc in filtered_sortclasses:
print(f'{sc.common_name}, {sc.tier}, {sc.min_price}, {print(sc.items)}')
input('Continue')
By accident instead of simply putting {sc.items}
, you used another print
function inside and f-string.
As the string gets compiled, the inner print
function gets executed before the outer one and so separately prints a part of the output while the rest of the output comes later when the outer function is executed.
So simply remove the inner print()
and all will be fine.
英文:
Your issue is right here in the print statement:
for sc in filtered_sortclasses:
print(f'{sc.common_name}, {sc.tier}, {sc.min_price}, {print(sc.items)}')
input('Continue')
By accident instead of simply putting {sc.items}
, you used another print
function inside and f-string.
As the string gets compiled, the inner print
function gets executed before the outer one and so separately prints a part of the output while the rest of the output comes later when the outer function is executed.
So simply remove the inner print()
and all will be fine.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论