#๐Ÿ”’ US Population Data Python Project

53 messages ยท Page 1 of 1 (latest)

tough canopyBOT
#

@shy rover

Python help channel opened

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.

shy rover
#

Hey guys

#

I need help with this python project

vernal turret
#

!paste

tough canopyBOT
#
Pasting large amounts of code

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.

shy rover
#

let me know if you can see my code or not

vernal turret
#

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

shy rover
#

and change it to what

vernal turret
#

!e

my_lines = ['one\n','two\n']

for line in range(len(my_lines)): print(line)

for line in my_lines: print(line)
tough canopyBOT
vernal turret
#

I'm assuming you're looking for the second one

shy rover
#

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

vernal turret
# shy rover ok

do you understand what us_population = int(line.strip().replace(',','')) does?

shy rover
#

yes

#

it replaces the , in each number in the file with an empty string meaning removes the commas

vernal turret
#

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

shy rover
#

yes ill try that

#

line would be an unresolved reference then

#

should i put this inside another for loop that iterates through the lines

vernal turret
#

oh

#

line would be us_population[i]

#

my bad forgot to switch that one

#

@shy rover

shy rover
#

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

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

shy rover
#

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

vernal turret
#

yes basically, range generates numbers. when you pass len(something) to it it will generate 0,1,2,3...,len(something)-1

shy rover
#

i added it back

vernal turret
#

you're looping over that and using it to index the list

shy rover
#

and my program works successfully

vernal turret
#

glad it works ๐Ÿ‘

late violet
#

ur a genius man

tough canopyBOT
#
Python help channel closed

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.