Python 将输入乘以平均值

Python 将输入乘以平均值,python,python-3.7,Python,Python 3.7,我想得到目的地的平均体重。所以它必须是平均的目的地。用户输入值为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 wei

我想得到目的地的平均体重。所以它必须是平均的目的地。用户输入值为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)

你的问题归结为:

    def avgMass():
        destination = "Mars"
        average = 0.32
        destweight = average * destination

您需要编写一个函数,该函数提示输入字符串目标,然后返回相应的浮点值。(可能通过在映射中查找来实现。)函数应该循环,直到给定有效的目标。

如果您试图对字符串执行数字操作,则需要将用户输入映射到有意义的数字值-可以有多种方法。我建议的一个更简单的方法是使用字典。下面是示例代码,或者你可以将目的地距离作为用户输入查看你当前的代码,我想你应该将行星名称作为输入

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()

添加一个递归函数,该函数检查
目标
直到可接受(数字)为止,比如说
浮点
。i、 e:

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()

“我知道float有一些错误”你到底得到了什么错误?不能用'float'类型的非整数乘以序列。
destwweight=average*destination
这里的
destination
不是字符串吗?你说的
destwweight=average*destination
是什么意思。谢谢你指出这一点肯定会有帮助:)我知道有很多方法可以解决这个问题。这一切都让我不知所措,哈哈。非常感谢