如何使变量显示为英寸或磅

时间:2017-12-23 04:30:08

标签: python-3.x

打印时,我必须将几个变量显示为磅和英寸。

while True:
name = input("\nEnter players name or, 0 to quit: ")
if name == "0":
    break

height = float(input("Enter players height in inches: "))

weight = float(input("Enter players weight in pounds: "))

bmi = weight * 703 / height * height

print("\nBMI Profile Of %s" % name,"Height: ",height,"Weight: ",weight,"BMI Index: ",bmi,sep = "\n")

所以我的问题是如何让变量高度显示为英寸或"和重量为磅。

2 个答案:

答案 0 :(得分:0)

使用str将这些整数转换为使用+:

添加附加字符串的字符串
print("\nBMI Profile Of %s" % name,"Height: ",str(height)+" inches","Weight: ",str(weight) + " lbs","BMI Index: ",bmi,sep = "\n")

输出:

BMI Profile Of Scott
Height: 
60 inches
Weight: 
200 lbs
BMI Index: 
140600.0

答案 1 :(得分:0)

您可以按照以下方式使用。并使用str进行汇总。高度为",重量为lbs

while True:
  name = input("\nEnter players name or, 0 to quit: ")
  if name == "0":
      break

  height = float(input("Enter players height in inches: "))

  weight = float(input("Enter players weight in pounds: "))

  bmi = weight * 703 / height * height

  print("\nBMI Profile Of %s" % name,"Height: ",str(height) +  '″', "Weight: ",str(weight) + " lbs","BMI Index: ",bmi,sep = "\n")

<强>输出

BMI Profile Of Test
Height: 
186.0″
Weight: 
75.0 lbs
BMI Index: 
52724.99