将输入乘以平均值

2024-09-29 06:29:50 发布

您现在位置:Python中文网/ 问答频道 /正文

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

Tags: forinputdestinationavailableprintaverageweight目的地
3条回答

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

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。i、 电子邮件:

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

你的问题归结为:

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

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

相关问题 更多 >