我很难用小数点打印数字。 当我运行python并要求它执行除法时,结果必须是一个带小数点的数字,它不会打印小数点。 例如,我刚刚制定了这个算法:
print 'Report card calculation'
grade1 = input ('Insert the first grade: ')
grade2 = input ('Insert the second grade: ')
grade3 = input ('Insrt the third grade: ')
media = (grade1 + grade2 + grade3) / 3
print 'Your final result is', media
但是当它打印“媒体”时,应该有小数点的数字不会带有小数点。我怎么能修好?
在文件顶部添加:
或者使用3.0而不是3。在python2中,将两个整数相除总是生成一个整数。在
最简单的更改:除以
3.0
而不是3
:这将确保分配给
media
的值将是浮点的,即使三个等级变量包含int。在你的运算被当作整数除法处理,试着用3.0代替3,看看会发生什么。在
相关问题 更多 >
编程相关推荐