my_input = int(my_input)
There is no shorter way than using the int function (as you mention)
my_input = int(my_input)
There is no shorter way than using the int function (as you mention)
Maybe you were hoping for something like my_number = my_input.to_int. But it is not currently possible to do it natively. And funny enough, if you want to extract the integer part from a float-like string, you have to convert to float first, and then to int. Or else you get ValueError: invalid literal for int().
The robust way:
my_input = int(float(my_input))
For example:
>>> nb = "88.8"
>>> int(nb)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '88.8'
>>> int(float(nb))
88
Videos
here is the code:
integer = 170
string = 'sadsad'
print(integer+int(string))
ps thx
Hello Everyone,
I was wondering if it's possible to get the input of a string from the user and convert it into an integer later on. I'm asking this as the code I'm working on requires a string input from the user of what service they'd like. I later had to add up the totals of the services and wondered if once the choice was selected or inputted then if it would become a number that later adds up to the total cost if multiple services are inputted.