r/learnpython • u/whealton • 14d ago
Been away from Python / PyCharm for a while - any ideas on this?
Everybody, let me prefix this by saying I've been away from Python for several years, but now I need to teach an introductory class. I'm encountering something very bizarre in JetBrains PyCharm. I keep thinking it's gotta be me (and it probably is), but I was wondering if any of you can pick this out?
Because I don't appear able to post a screen snip, I'll post the code.
list_2d = [["1", "Wheel Burrow", 29.00], # A 2D list, different data types (string / float)
["2", "Jack Hammer", 594.29],
["3", "Power Drill", 39.99]]
for j in range(0, len(list_2d), 1): # Iterate through our 2D list
print(f"Item Number: {list_2d[j][0]:<3}\tItem Name: {list_2d[j][1]:<15}\tItem Cost: $ {float(list_2d[j][2]):>6.2f}")
Item Number: 1 Item Name: Wheel Burrow Item Cost: $ 29.00
Item Number: 2 Item Name: Jack Hammer Item Cost: $ 594.29
Item Number: 3 Item Name: Power Drill Item Cost: $ 39.99Now, you're probably saying to yourself, "why are you casting the float to a float?" I hear you loud and clear. Here's the thing - if I don't do that, PyCharm is flagging it as "format code f not supported for str," but here's the thing - it still prints it correctly as shown above.
This was the original line and the >6.2f is what is being flagged.
print(f"Item Number: {list_2d[j][0]:<3}\tItem Name: {list_2d[j][1]:<15}\tItem Cost: $ {list_2d[j][2]:>6.2f}")I knew I had been away for a while, but geez. This is PyCharm 2026.2.3 using Python 3.14.7 under Windows 11. Am I missing anything? Note that I also go into a single dimension list but even if I comment all that code out, nothing changes. Thank you in advance for any potential ideas on where I probably goofed it.
6
u/AlexMTBDude 14d ago
That's not the Pythonic way to do a for loop. For loops don't count in Python.
Something like this instead:
for inner_list in list_2d:
print(f"Item Number: {inner_list[0]:<3}\tItem Name: {inner_list[1]:<15}\tItem Cost: $ {inner_list[2]:>6}")3
u/whealton 14d ago edited 14d ago
I can certainly take a look at that. UPDATE: I'm incorporating this. Thanks!
2
u/KidTempo 10d ago edited 10d ago
I would have used:
for num, name, cost in list_2d: print(f"Item Number: {num:<3}\tItem name: {name:<15}\tItem Cost: $ {float(cost):>6.2f}")(more pythonic but of course doesn't teach the concept of a 2d indexed list)
Personal preference, quick dataclass - it lets you enforce type safety:
from dataclasses import dataclass class Item: number: int name: str cost: float for i in [Item(*item) for item in list_2d]: print(f"Item Number: {i.number:<3}\tItem name: {i.name:<15}\tItem Cost: {i.cost:>6.2f}")
6
u/Pythagorean_1 14d ago
This doesn't look like you've been away from python but rather like you've never been there
1
2
u/fakemoose 14d ago
Unrelated to your question, but what’s the point of using a list instead of a dictionary?
Is this supposed to be demonstrating list comprehension?
1
u/whealton 14d ago
Just demonstrating list s- period. Nothing fancy. Single and 2D. It's only their second week.
2
u/gdchinacat 13d ago
You might want to check the spelling of "wheelbarrow" before putting this in front of students.
2
0
14d ago
[deleted]
2
u/whealton 14d ago edited 14d ago
While they're not doing any collection structures beyond lists this week, I can incorporate it into that and see if it makes a difference and let them know why. UPDATE: I'm incorporating this. Thanks!
0
14d ago
[deleted]
1
u/whealton 14d ago
I added it. I'm going to do it with both 2D lists and outer list / inner tuples. I think they'll be able to handle it. I just don't want to go all cowboy and off track on what they normally teach them.
2
5
u/TheCozyRuneFox 14d ago
From what I can find this is because charm thinks the inner list values are either string or float. It doesn’t track type by position.
Also you shouldn’t be using lists for multiple type collections. Tuples are standard for that use. You could also use a data class.