将输入乘以平均值

时间:2019-10-15 11:30:36

标签: 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 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)

3 个答案:

答案 0 :(得分:0)

您的问题归结为:

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

您需要编写一个函数,提示输入字符串目标,然后返回相应的浮点数。 (可能是通过在地图中查找它。)该函数应循环运行,直到给出有效的目的地为止。

答案 1 :(得分:0)

您尝试对字符串进行数值运算,您需要将用户输入映射到有意义的数值-可以有多种方式。我建议的一种更简单的方法是使用字典。下面的示例代码或您显然可以将目的地距离作为用户输入查看当前代码,我认为您希望将行星名称作为输入。

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

答案 2 :(得分:0)

添加一个递归函数,检查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()