我已经获得指令“编写一个程序,要求用户输入质量和速度值,然后调用 kinetic_energy 函数来获得对象的动能。”
我编写的代码在逻辑上是正确的,但我想知道我的语法是否向后,然后说明中描述了什么。我相信我的 main 函数正在计算结果,只是将其传递给 kinetic_energy 函数。我相信说明书要求以相反的顺序完成。如果我的假设是正确的,我将如何重写它以遵守指示?
#Gets the inputs from the user for mass and velocity
def main():
mass = int(input('Please enter the objects mass in kilograms: '))
velocity = int(input('Please enter the objects velocity in meters per second: '))
#Calculates the kinetic energy
kinetic_energy(ke = .5 * mass * velocity ** 2)
#Calculates the kinetic energy
def kinetic_energy(ke):
print ('The amount of kenetic energy is ',format(ke,',.2f'),sep='')
main()
答案 0 :(得分:2)
您将kinetic_energy
定义为一个函数,它接受两个参数,质量和速度,然后返回计算值:
def kinetic_energy(m, v):
return .5 * m * v ** 2
然后从main
调用该函数:
def main():
mass = int(input('Mass in kilograms: '))
velocity = int(input('Velocity in m/s: '))
ke = kinetic_energy(mass, velocity)
print('Kinetic energy is {.2f}'.format(ke))
main()
答案 1 :(得分:1)
通常,您编写的功能是" work"。
因此,如果你有一个var indexName = "index-name";
var mustClauses = new List<QueryContainer>();
var filterClauses = new List<QueryContainer>();
mustClauses.Add(new TermQuery
{
Field = new Field("description"),
Value = "white"
});
filterClauses.Add(new NumericRangeQuery
{
Field = new Field("price"),
LessThanOrEqualTo = 3000,
GreaterThanOrEqualTo = 2000
});
var searchRequest = new SearchRequest<Product>(indexName)
{
Size = 10,
From = 0,
Query = new BoolQuery
{
Must = mustClauses,
Filter = filterClauses
}
};
var searchResponse = client.Search<Product>(searchRequest);
函数,那么你可以使用该函数来完成比打印更多的工作。
所以,更适合重写你的东西:
kinetic_energy