如何在python 3.x中打印用户输入

时间:2018-07-17 07:44:40

标签: python-3.x python-2.7

如何以以下格式在python 3.x中打印以下行。

print ("How old are you?"),
age = input ()
print ("How tall are you ?"),
height = input ()
print ("How much do you weigh?"),
weight = input()

print("so , you're % year old, % ft tall and % kg heavy.")% (age,height,weight)

错误:-

How old are you?
35
How tall are you ?
2
How much do you weigh?
23

所以,您今年%x岁,身高%x英尺,体重%x kg。

  

回溯(最近通话最近一次):“ C:/ Narendra / 8th July Ethans”   “ Python Batch / Exercises / Python3 / ex11.py”,第8行,在       print(“所以,您%x岁,%x ft高,%x kg磅。”)%(年龄,身高,体重)TypeError:%不支持的操作数类型:   'NoneType'和'tuple'

3 个答案:

答案 0 :(得分:3)

如果您使用的是Python> = 3.6,请使用f-string

例如:

age = input ("How old are you?\n");

height = input ("How tall are you ?\n");

weight = input("How much do you weigh?\n");

print(f"so , you're {age} year old, {height} ft tall and {weight} kg heavy.")

str.format

例如:

print("so , you're {0} year old, {1} ft tall and {2} kg heavy.".format(age, height, weight))

答案 1 :(得分:1)

格式运算符%对字符串起作用,而不对整个print函数起作用,该函数始终返回None。另外,您需要使用诸如%s之类的规范来指定占位符的格式。

print("so , you're %s year old, %s ft tall and %s kg heavy." % (age,height,weight))

答案 2 :(得分:1)

我会这样使用.format()函数;

age = input('How old are you? ')
height = input('How tall are you? ' )
weight = input('How much do you weigh? ')

print(("so, you're {} year old, {} ft tall and {} kg heavy").format(age,height,weight))