[英]Need help adding text after an output in python
I want someone to input their weight in lbs which is then converted to kg.我希望有人以磅为单位输入他们的体重,然后将其转换为公斤。 However the output is just the converted number.然而 output 只是转换后的数字。 I can't figure out how to label the output with "kg".我不知道如何用“kg”来 label output。 ex: 72 kg例如:72 公斤
weight_lbs = input('Weight (lbs): ')
weight_kg = int(weight_lbs) * 0.45
kg = 'kg'
print(weight_kg + kg)
you need to do str(weight_kg)
to make it a string type (instead of a number)你需要做str(weight_kg)
使其成为字符串类型(而不是数字)
weight_lbs = input('Weight (lbs): ')
weight_kg = int(weight_lbs) * 0.45
kg = 'kg'
print(str(weight_kg) + kg)
You can also just use %s and get rid of 'kg' = kg您也可以只使用 %s 并摆脱 'kg' = kg
print("%s %s" % (weight_kg,"kg"))
Another version of the twice upvoted answer:两次投票的答案的另一个版本:
weight_lbs = input('Weight (lbs): ')
weight_kg = str(int(weight_lbs) * 0.45)
kg = 'kg'
print(weight_kg + kg)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.