If you have only an integer in the conditional of an if statement, the if statement will run if and only if the integer is not equal to zero. You need to do
if num < 0:
not
if num:
But indeed, @user8145959 has a point. The number inputted is already a string. When you pass the input string to int_to_str, it gets automatically converted to an integer at the places where you try integer operations on it.
Edit: Automatic integer conversion works only in python 2, not python 3. My mistake.
Edit2: Actually I'm just wrong. Didn't realize input() did the conversion already.
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.
If you have only an integer in the conditional of an if statement, the if statement will run if and only if the integer is not equal to zero. You need to do
if num < 0:
not
if num:
But indeed, @user8145959 has a point. The number inputted is already a string. When you pass the input string to int_to_str, it gets automatically converted to an integer at the places where you try integer operations on it.
Edit: Automatic integer conversion works only in python 2, not python 3. My mistake.
Edit2: Actually I'm just wrong. Didn't realize input() did the conversion already.
The idea is to use the ASCII value of the digits from 0 to 9 start from 48 – 57.
num = 56
s = [0 if not num else '']
while num:
s.append(chr(48 + num % 10))
num = num // 10
a= "".join(reversed(s))
print(a)
print(type(a))
output->
56
<class 'str'>
Videos
Refer to a basic atoi in C:
int myAtoi(char *str)
{
int res = 0; // Initialize result
// Iterate through all characters of input string and update result
for (int i = 0; str[i] != '\0'; ++i)
res = res*10 + str[i] - '0';
// return result.
return res;
}
Which translates into the Python:
def atoi(s):
rtr=0
for c in s:
rtr=rtr*10 + ord(c) - ord('0')
return rtr
Test it:
>>> atoi('123456789')
123456789
If you want to accommodate an optional sign and whitespace the way that int does:
def atoi(s):
rtr, sign=0, 1
s=s.strip()
if s[0] in '+-':
sc, s=s[0], s[1:]
if sc=='-':
sign=-1
for c in s:
rtr=rtr*10 + ord(c) - ord('0')
return sign*rtr
Now add exceptions and you are there!
This is really inefficient but:
>>> zero = ord("0")
>>> s = "1234"
>>> sum([x * 10**i for i, x in enumerate(map(lambda x: x - zero, map(ord, s))[::-1])])
1234
This is slightly better:
>>>> sum([x * 10**i for i, x in enumerate([ord(x) - zero for x in s[::-1]])])
1234
>>> atoi = lambda s: sum([x * 10**i for i, x in enumerate([ord(x) - zero for x in s[::-1]])])
>>> atoi("1234")
1234