将输入乘以平均值
multiplying an input by an average
我正在尝试获取目的地的平均重量。所以它需要是平均 * 目的地。用户输入目的地 say mars,其值为 0.377。我怎样才能让这段代码工作。我知道浮动有一些错误。我该如何修改此代码。
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)
您的问题归结为:
def avgMass():
destination = "Mars"
average = 0.32
destweight = average * destination
您需要编写一个函数来提示输入字符串目标,然后 returns 相应的浮点数。 (可能通过在地图中查找它。)该函数应该循环直到给出一个有效的目的地。
您正在尝试对字符串执行数值运算,您需要将用户输入映射到有意义的数值 - 可以有多种方法来执行此操作。我建议的一种更简单的方法是使用字典。下面的示例代码或者您显然可以将目的地距离作为用户输入查看您当前的代码,我认为您想将行星名称作为输入。
def avgMass():
user_input = input("Please enter destination: ")
dict_dest_mapping = {'Mars':0.377 , 'Venus':0.55}
if user_input in dict_dest_mapping.keys():
destination = dict_dest_mapping[user_input]
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)
if __name__ == '__main__':
avgMass()
添加一个递归函数来检查 destination
直到可接受(数字)让我们说 float
。即:
def get_destination():
try:
destination = float(input("Please enter destination: "))
return destination
except:
return get_destination()
def avgMass():
destination = get_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)
avgMass()
我正在尝试获取目的地的平均重量。所以它需要是平均 * 目的地。用户输入目的地 say mars,其值为 0.377。我怎样才能让这段代码工作。我知道浮动有一些错误。我该如何修改此代码。
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)
您的问题归结为:
def avgMass():
destination = "Mars"
average = 0.32
destweight = average * destination
您需要编写一个函数来提示输入字符串目标,然后 returns 相应的浮点数。 (可能通过在地图中查找它。)该函数应该循环直到给出一个有效的目的地。
您正在尝试对字符串执行数值运算,您需要将用户输入映射到有意义的数值 - 可以有多种方法来执行此操作。我建议的一种更简单的方法是使用字典。下面的示例代码或者您显然可以将目的地距离作为用户输入查看您当前的代码,我认为您想将行星名称作为输入。
def avgMass():
user_input = input("Please enter destination: ")
dict_dest_mapping = {'Mars':0.377 , 'Venus':0.55}
if user_input in dict_dest_mapping.keys():
destination = dict_dest_mapping[user_input]
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)
if __name__ == '__main__':
avgMass()
添加一个递归函数来检查 destination
直到可接受(数字)让我们说 float
。即:
def get_destination():
try:
destination = float(input("Please enter destination: "))
return destination
except:
return get_destination()
def avgMass():
destination = get_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)
avgMass()