如何在冒号后得到产品

how to get the product after the colons

所以我正在学习这个 BMI 计算器程序,我希望答案写在“:”之后,而不是在它下面。谁能帮我吗?甚至有可能得到那种类型的答案吗?

代码如下:

name = "hello"
height_m = 2
weight_kg = 110

bmi = weight_kg / (height_m ** 2)
print("bmi:")
print(bmi)
if bmi < 25:
    print(name)
    print("is not overweight")
else:
    print(name)
    print("is overweight")

#printed:
bmi:
27.5
hello
is overweight

#but what i want is this:
bmi: 27.5
hello is overweight

试试这个

name = "hello"
height_m = 2
weight_kg = 110

bmi = weight_kg / (height_m ** 2)
print("bmi: {}".format(bmi)
if bmi < 25:
    print("{} is not overweight".format(name))
else:
    print("{} is overweight".format(name))

#prints:
bmi: 27.5
hello is overweight

您需要在同一个 print 语句中放置一个逗号,而不是两个单独的语句。

print("bmi:",bmi)

而不是

print("bmi:")
print(bmi)

试试这个,

name = "hello"
height_m = 2
weight_kg = 110

bmi = weight_kg / (height_m ** 2)
print("bmi:", end=' ')
print(bmi)
if bmi < 25:
    print(name, end=' ')
    print("is not overweight")
else:
    print(name, end=' ')
    print("is overweight")

或者您可以像这样将所有内容放在一个打印语句中,

print(name, "is overweight")
print("bmi:", bmi)

您可以:

print("bmi:", bmi) # the simplest

print(f"bmi: {bmi}") # f string in Python 3

print("bmi: ", end="") # Your original code, modified
print(bmi)

print("bmi: %s " % bmi) # too extreme -- don't do it for this simple case

print("bmi: {}".format(bmi)) # too extreme