#๐ US Population Data Python Project
53 messages ยท Page 1 of 1 (latest)
@shy rover
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
!paste
If your code is too long to fit in a codeblock in Discord, you can paste your code here:
https://paste.pythondiscord.com/
After pasting your code, save it by clicking the Paste! button in the bottom left, or by pressing CTRL + S. After doing that, you will be navigated to the new paste's page. Copy the URL and post it here so others can see it.
line is an int
you can't call strip on it
what are you trying to do there?
nvm I think I see
you should remove the range len part
and change it to what
!e
my_lines = ['one\n','two\n']
for line in range(len(my_lines)): print(line)
for line in my_lines: print(line)
:white_check_mark: Your 3.12 eval job has completed with return code 0.
001 | 0
002 | 1
003 | one
004 |
005 | two
I'm assuming you're looking for the second one
ok
C:\Users\subha\PycharmProjects\Chapter7ProgrammingExercises.venv\Scripts\python.exe C:\Users\subha\PycharmProjects\Chapter7ProgrammingExercises\us_population_data.py
Traceback (most recent call last):
File "C:\Users\subha\PycharmProjects\Chapter7ProgrammingExercises\us_population_data.py", line 44, in <module>
main()
File "C:\Users\subha\PycharmProjects\Chapter7ProgrammingExercises\us_population_data.py", line 12, in main
total_years = len(us_population)
^^^^^^^^^^^^^^^^^^
TypeError: object of type 'int' has no len()
Process finished with exit code 1
now i am getting this error
when i do that
do you understand what us_population = int(line.strip().replace(',','')) does?
yes
it replaces the , in each number in the file with an empty string meaning removes the commas
it sets the variable to that, and converts it to an int
but you set the same variable over and over in the loop
so it just ends up having the last one in it
maybe you wanted
for i in range(len(us_population)):
us_population[i] = int(line.strip().replace(',',''))
```?
that will replace all the lines in that list with their integer counterparts
yes ill try that
line would be an unresolved reference then
should i put this inside another for loop that iterates through the lines
ok
C:\Users\subha\PycharmProjects\Chapter7ProgrammingExercises.venv\Scripts\python.exe C:\Users\subha\PycharmProjects\Chapter7ProgrammingExercises\us_population_data.py
Traceback (most recent call last):
File "C:\Users\subha\PycharmProjects\Chapter7ProgrammingExercises\us_population_data.py", line 44, in <module>
main()
File "C:\Users\subha\PycharmProjects\Chapter7ProgrammingExercises\us_population_data.py", line 9, in main
us_population[i] = int(us_population[i].strip().replace(',', ''))
~~~~~~~~~~~~~^^^
TypeError: list indices must be integers or slices, not str
Process finished with exit code 1
@vernal turret
.
I'm assuming you didn't add the range(len(...)) back
do you know what that part is doing?
I think it'd be better to explain than to blindly fix your program for you
oh no i accidentally removed it
it iterates through the length of the list
instead of trying to iterate through a whole file and causing errors
yes basically, range generates numbers. when you pass len(something) to it it will generate 0,1,2,3...,len(something)-1
i added it back
you're looping over that and using it to index the list
and my program works successfully
glad it works ๐
ur a genius man
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.