How To Print int Values
Question:
So, I am making a test SCII adventure game and when i need to print out the enemy HP it tells me I can’t print int values with str values.
Code:
enemy_hp=73
print("Enemy HP: " + enemy_hp)
Result:
TypeError: can only concatenate str (not "int") to str
Wanted result:
Enemy HP: 73
Can someone help me out pls.
Answers:
print(f'Enemy HP: {enemy_hp}')
You can use .format to do it. Such as below:
enemy_hp=73
print("Enemy HP: " + "{ }".format(enemy_hp))
This happens because you are concatenating int and strings when printing out
You can solve this in various ways
- using a format string
print(f'Enemy HP: {enemy_hp}')
- converting int to str
print('Enemy HP: '+ str(enemy_hp))
str cant + int!
so do this
print(f"Enemy HP: " {enemy_hp})
or do this
print(("Enemy HP: + {}").format(enemy_hp))
So, I am making a test SCII adventure game and when i need to print out the enemy HP it tells me I can’t print int values with str values.
Code:
enemy_hp=73
print("Enemy HP: " + enemy_hp)
Result:
TypeError: can only concatenate str (not "int") to str
Wanted result:
Enemy HP: 73
Can someone help me out pls.
print(f'Enemy HP: {enemy_hp}')
You can use .format to do it. Such as below:
enemy_hp=73
print("Enemy HP: " + "{ }".format(enemy_hp))
This happens because you are concatenating int and strings when printing out
You can solve this in various ways
- using a format string
print(f'Enemy HP: {enemy_hp}')
- converting int to str
print('Enemy HP: '+ str(enemy_hp))
str cant + int!
so do this
print(f"Enemy HP: " {enemy_hp})
or do this
print(("Enemy HP: + {}").format(enemy_hp))