Python 3.x 如何在Python3.x中打印用户输入
如何在Python3.x中以下面的格式打印下面的行Python 3.x 如何在Python3.x中打印用户输入,python-3.x,python-2.7,Python 3.x,Python 2.7,如何在Python3.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)
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)
错误:-
那么,你是%x岁、%x英尺高、%x千克重
回溯(最近一次调用):文件“C:/Narendra/Ethans 7月8日
Python批处理/Exercises/Python3/ex11.py”,第8行,在
打印(“因此,您是%x岁、%x英尺高、%x千克重。”)%(年龄、身高、体重)类型错误:不支持%的操作数类型:
“非类型”和“元组”
格式运算符
%
处理字符串,而不是整个print
函数,该函数始终返回None
。此外,还需要使用规范(如%s
)指定占位符的格式
print("so , you're %s year old, %s ft tall and %s kg heavy." % (age,height,weight))
如果您使用的是Python>=3.6:请使用
f-string
Ex:
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.")
print("so , you're {0} year old, {1} ft tall and {2} kg heavy.".format(age, height, weight))
或str.format
Ex:
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.")
print("so , you're {0} year old, {1} ft tall and {2} kg heavy.".format(age, height, weight))
我会像这样使用.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))
完美工作:你多大了?你有多高?你体重多少?45岁,你23岁,3英尺高,45公斤重