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
Okay, so I had a crash course in Python a looooong time ago, and that's all the prior experience I have with it. I'm starting to dabble in it again, playing around with it to make a text-based game.
In this game, you have stats - Speed, Health, etc etc. Each of these is a number (an int).
I am trying to define a function names statdisplay() so that when I call on it, it prints out your stats. So, if you have a Speed of 1, and the max Speed you can increase that stat to is 5, one of the printed lines would be:
Speed: 1 / 5
This was my ORIGINAL code:
print("\nHealth: " + healthstat + " / " + maxhealth)
print("\nHunger: " + hungerstat + " / " + maxhunger)
print("\nStalking Skill: " + stalkingstat + " / 5")
print("\nHunting Skill: " + huntingstat + " / 5")
print("\nSpeed: " + speedstat + " / 5")
print("\nStrength: " + speedstat + " / 5")But then I got the following error:
TypeError: can only concatenate str (not "int") to str
So I mentally facepalmed myself and looked up how to convert an int to a string, and I keep reading that you can use the str() function (I know there are other ways, but I'm taking baby steps here trying to jog my memory on how everything works before I go doing everything by what's deemed "most appropriate").
This is my NEW code with that in mind:
print("\nHealth: " + str(healthstat) + " / " + maxhealth)
print("\nHunger: " + str(hungerstat) + " / " + maxhunger)
print("\nStalking Skill: " + str(stalkingstat) + " / 5")
print("\nHunting Skill: " + str(huntingstat) + " / 5")
print("\nSpeed: " + str(speedstat) + " / 5")
print("\nStrength: " + str(speedstat) + " / 5")...and yet I am still getting the following error:
TypeError: can only concatenate str (not "int") to str
I can't seem to figure out what's wrong. I do not have the str() function defined as anything else. str() also doesn't seem to come from any special libraries that I'd need to import, but maybe I'm wrong there (I tried to look it up)... the only import I currently have is "import random".
My only other thought is that maybe it's a Google Colab thing, as that's where I'm currently running my code since this wasn't anything serious and I tend to go between two different computers.
Any help would be much appreciated!
Edit: accidentally had the new code in both code boxes.
here is the code:
integer = 170
string = 'sadsad'
print(integer+int(string))
ps thx