This question already has answers here:
How to convert a string to a number if it has commas in it as thousands separators?
(10 answers)
Closed 5 years ago.
pop=x.nextSibling()[0] # pop="Population: 1,414,204"
pre_result=pop.text.split(" ") #pre_result=['Population:','1,414,204']
a=pre_result[1] # a='1,414,204'
result=int(a) #Error pops over here.Tried int(a,2) by searching answers in internet still got an error.
print(result)
print(type(result))
The following is the error message.I thought typecasting from str to int would be simple but still i came across this error.I am a beginner in python so sorry if there has been any silly mistake in my code.
File "C:\Users\Latheesh\AppData\Local\Programs\Python\Python36\Population Graph.py", line 14, in getPopulation
result=int(a)
ValueError: invalid literal for int() with base 10: '1,373,541,278'
The error is self explaining, a contains the string '1,373,541,278', and that is not a format Python can handle.
We can however remove the comma's from the string, with:
result=int(a.replace(',', ''))
But it is possible that for some elements, you will have to do additional processing.
Related
This question already has answers here:
Difference between int() and long()
(1 answer)
What are metaclasses in Python?
(25 answers)
Closed 1 year ago.
I am trying to use
https://github.com/iandees/mongosm/blob/master/insert_osm_data.py
this package. It seems like it is written in Python2. I have converted all the way to next(context). However, I am getting name 'long' is not defined.
Is there any way that I can define this somewhere? How can I define 'long' and I have no idea what this is for even for Python2 Script (which worked fine somehow).
long() is basically renamend to int() in Python 3.
Please see https://www.python.org/dev/peps/pep-0237/ for details.
So, either do a search of long and replace with int, or define it
long = int
somewhere at the beginning of your file.
You should convert all code to Python3
https://www.google.com/search?channel=fs&client=ubuntu&q=Converting+python2+to+python3
https://docs.python.org/3/library/2to3.html
This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 2 years ago.
enter image description here
I wrote a simple code in python.
Originally my assignment is to receive two inputs(name of person) and print them.
Here's my question.
When I try to sum two variables but one of them is int and another one is str, an error occurs.
But in this case (the picture) why variable 'a' is recognized as a str not int?
I think there must occurs an error but a is recognized as a str and work well.
In Python 3, input() always returns a string. (In Python 2, input() would try to interpret – well, evaluate, actually – things, which was not a good idea in hindsight. That's why it was changed.)
If you want to (try to) make it an int, you'll need int(input(...)).
This question already has answers here:
How to print without a newline or space
(26 answers)
Closed 2 years ago.
I've begun my first scripting class. I'm am currently stuck in the string formatting section.
The instructions for this problem are, "Write a single statement to print: user_word,user_number. Note that there is no space between the comma and user_number."
They provide part of the code to start,
user_word = str(input())
user_number = int(input())
I've had trouble getting errors combining strings and integers in single statements and I am a bit lost on where to start on this. This is also my first time on stack overflow.
You can do it like this
print(user_word+","+str(user_number))
It should work.
This way, you cast you int to string and then you concatenate.
This question already has answers here:
Python TypeError must be str not int [duplicate]
(3 answers)
Closed 3 years ago.
I'm trying to write a basic Python RPG for my friend, and I'm trying to make a stat generator. From what I learned in classes, I'm supposed to print('Your ability score in this is ' + var1 + '.')
Here's the entire code block where I'm getting the error.
Your elemental attack is '''+ elematk + '''.''')
And the error I'm getting is
File "/Users/data censored/Desktop/Basic RPG.py", line 24, in <module>
Your elemental attack is '''+ elematk + '''.''')
TypeError: can only concatenate str (not "int") to str
Maybe I'm just bad at this. I looked at another answer saying that commas separate a string and a var, but that didn't work either.
Your elematk variable is an integer, hence you can't concatenate it with a string.
You can case it to a string type by doing str(elematk)
Try doing something like:
print('elemental attack is '''+ str(elematk)+ '''.''')
This question already has answers here:
Python: Invalid Token
(3 answers)
Closed 8 years ago.
Today I am learning python and I have assign one int variable in console like this
zipcode = 02492
But it's return me error like this
SyntaxError: invalid token
Why so, I don't understand it? Please help me to solve this query.
The reason you're getting the error is because Python interprets a number starting with the digit 0 as octal (base 8). However, the only valid octal digits are 0-7, so the 9 in your zip code is seen as invalid. Additionally, if you're using Python 3, the format of octal literals was changed so they begin with 0o now (a zero followed by the lowercase letter o), so you'd still get an error even if you tried to input zipcode = 02432, which would be valid in Python 2.
Since a zip code doesn't need to mathematical operations performed on it, it would be best if it was stored as a string.