Python 编写一个执行数学计算的程序

Python 编写一个执行数学计算的程序,python,math,decimal,computation,Python,Math,Decimal,Computation,我正在上有史以来的第一节编程课,第二个实验室已经在踢我的屁股了 教授希望我们编写一个程序,从用户那里获得以英尺为单位的十进制测量值(输入),然后分别以英尺和英寸为单位显示该数字(输出) 我对如何开始感到困惑。我们的结果是“10.25英尺等于10英尺3英寸” 这就是我到目前为止所做的: print ("This program will convert your measurement (in feet) into feet and inches.") print () # input sect

我正在上有史以来的第一节编程课,第二个实验室已经在踢我的屁股了

教授希望我们编写一个程序,从用户那里获得以英尺为单位的十进制测量值(输入),然后分别以英尺和英寸为单位显示该数字(输出)

我对如何开始感到困惑。我们的结果是“10.25英尺等于10英尺3英寸”

这就是我到目前为止所做的:

print ("This program will convert your measurement (in feet) into feet and inches.")
print ()

# input section
userFeet = int(input("Enter feet: "))

# conversion
feet = ()
inches = feet * 12

#output section
print ("You are",userFeet, "tall.")
print (userFeet "feet is equivalent to" feet "feet" inches "inches")
我不知道从这里到哪里去。我理解将英尺转换为英寸,反之亦然。但我不明白如何分别从英尺转换到英尺和英寸


如果可以的话,请帮忙!谢谢

您将打印的脚只是userFeet的整数部分。英寸是转换后的十进制数。就像200分钟是3小时20分钟☺. 因此:

from math import floor
print ("This program will convert your measurement (in feet) into feet and inches.")
print ()

# input section
userFeet = float(input("Enter feet: "))

# conversion
feet = floor(userFeet) # this function simply returns the integer part of whatever is passed to it
inches = (userFeet - feet) * 12

#output section
print("You are {} tall.".format(userFeet))
print("{0} feet is equivalent to {1} feet {2:.3f} inches".format(userFeet, feet, inches))
几点提示:

userFeet不能是整数(因为它包含小数)

英尺数可以从float/double类型调整为整数

英寸数可以通过将userFeet和feet的差值(
userFeet-feet
)乘以12并四舍五入到最接近的整数来计算

10.25 feet => 10feet + 0.25 feet => 10feet and 0.25*12inch => 10feet 3inch

因此,取小数部分,乘以12,以英寸为单位,然后将英尺数和英寸数一起显示。

我将尝试通过对我所做更改的注释来修复您的代码

import math # This is a module. This allows you to access more commands. You can see the full list of "math" commands with this link: https://docs.python.org/2/library/math.html

print ("This program will convert your measurement (in feet) into feet and inches.")
print ()

# input section
userFeet = float(input("Enter feet: ")) # You used integer, which would not support decimal numbers. Float values can, and it is noted with float()

# conversion
feet = math.floor(userFeet) # This uses the floor command, which is fully explained in the link.
inches = (userFeet - feet) * 12

#output section
print ("You are", str(userFeet), "tall.")
print (userFeet, "feet is equivalent to", feet, "feet", inches, "inches") # You forgot to add the commas in the print command.
看看