我想得到目的地的平均体重。所以它必须是平均的目的地。用户输入目的地,比如火星,其值为0.377。我怎样才能让这个代码工作。我知道float有错误。我怎样才能破解这个密码。你知道吗
def avgMass():
destination = input("Please enter destination: ")
Mars = 0.377
a, b, c, d, e, f = [int(a) for a in input("Enter astronaut weights seperated by a
space: ").split()]
weights = a, b, c, d, e, f
crewweight = 100
specialistweight = 150
available = (crewweight - a, crewweight - b, crewweight - c,
specialistweight - d, specialistweight - e, specialistweight - f)
sumofmass = sum(available)
average = sumofmass / len(weights)
destweight = average * destination
print("Available weight for astronauts: ", available)
print("Total available weight: ", sumofmass, "kg")
print("Average available weight: ", average, "kg")
print("Average available weight on destination: ", destweight)
如果您试图对字符串执行数字操作,则需要将用户输入映射到有意义的数字值—可以有多种方法。我建议的一个更简单的方法是使用字典。下面的示例代码或者你可以直接把目的地的距离作为用户输入看看你当前的代码我想你应该把行星的名字作为输入。你知道吗
添加一个递归函数,检查
destination
直到可以接受(数字),比如说float
。i、 电子邮件:你的问题归结为:
您需要编写一个函数来提示输入字符串目标,然后返回相应的浮点值。(可能是通过在地图中查找)函数应该循环,直到给定一个有效的目标。你知道吗
相关问题 更多 >
编程相关推荐