0

just got into python, any advice why these simple lines sends me an error?

# ---------------- WEIGHT CONVERTER ----------------#

_userWeight = input("Weight [KG]: ")
_userLbs = int(_userWeight) * 2.2
print(int(_userWeight) + "[KG] = " + int(_userLbs) + "[lbs]")
martineau
  • 119,623
  • 25
  • 170
  • 301
Kennyair
  • 21
  • 2

2 Answers2

2
  _userWeight = input("Weight [KG]: ")
  _userLbs = int(_userWeight) * 2.2 
  print(int(_userWeight) , "[KG] = " , int(_userLbs) , "[lbs]")

Remove the '+' and use a ','

Output:

Weight [KG]: 50

50 [KG] = 110 [lbs]

PSR
  • 249
  • 1
  • 10
0

You cannot concatenate both string and integer data types.

You can however present the values as a string:

_userWeight = input("Weight [KG]: ")
_userLbs = int(_userWeight) * 2.2
print(str(_userWeight) + "[KG] = " + str(_userLbs) + "[lbs]")

# 80[KG] = 176.0[lbs]
Solebay Sharp
  • 519
  • 7
  • 24
  • _mix_ is unclear term in this case – buran Jan 23 '22 at 08:40
  • _You cannot print both strings and integers on a single lin_ this becomes worse with every iteration... – buran Jan 23 '22 at 08:44
  • Well I amended it prior. Is it now tolerable? – Solebay Sharp Jan 23 '22 at 08:45
  • It's not at all about printing. It's about **concatenating** `str` and `int` objects. They can as well try to construct a `str` by concatenating `str` and `int` and bind it to a name and it will fail all the same. e.g. `result = int(_userWeight) + "[KG] = " + int(_userLbs) + "[lbs]"`. And it's completly different story if they need `int()` at all. – buran Jan 23 '22 at 08:47
  • Yeah.. it is absolutely about printing though. That's actually the crux of the question. – Solebay Sharp Jan 23 '22 at 08:49
  • No, the problem and question is about the concatenating. It just happens that they construct the string and print it in one-line. It may well be a return statement in a function. – buran Jan 23 '22 at 08:50
  • 'It may well be' cuts in any number of directions. – Solebay Sharp Jan 23 '22 at 08:54
  • 2
    Just look at the other answer by @PSR and you will learn how to print `str` and `int` in one line. Of course `f-string` and `str.format()` are always preferable. – buran Jan 23 '22 at 08:56
  • Can't argue with that. – Solebay Sharp Jan 23 '22 at 09:16