我对编码非常陌生,偶然发现了我不知道如何解决的第一个问题:
weight_in_pounds=(input('How much pounds do you weigh? '))
weight_in_kilograms=weight_in_pounds*int(0.45)
print('You weigh '+ weight_in_kilograms +' kg.')
这是我输入的内容,这是我运行它的结果:
How much pounds do you weigh? 213
You weigh kg.
我不知道为什么它没有显示答案。在此示例中,我键入了213,但没有显示结果而是显示一个空格。我做错了什么?
答案 0 :(得分:2)
您正在将0.45转换为整数。整数0.45为0。weight_in_pounds * 0 = 0。
您正在输入weight_in_pounds,这使其成为字符串。 Python对于类型有点奇怪,因此字符串* 0只是一个空字符串。
您应该首先在第二行上删除到整数的转换,然后在第一行上添加到浮点数(十进制)的转换。即:weight_in_pounds=float(input('How much pounds do you weigh? '))
答案 1 :(得分:1)
只需很小的更改,您就可以使用它:
weight_pound = int(input('How much do you weigh in pounds? '))
weight_kg = weight_pound*0.45
print('You weigh {0} kg.'.format(weight_kg))
问题正像Brian提到的那样。这是类型问题。 另外,您只能连接字符串,因此必须使用这种格式来显示它。
*
Repetition - Creates new strings, concatenating multiple copies of the same string
Explaination of operations on strings
您将同一字符串重复0次。