如何通过for循环获取用户在字典中输入键的值?

时间:2016-04-05 04:19:16

标签: python dictionary

我正在尝试简化此代码,以计算每月总重量损失和平均重量损失:

months = input("How many months have you been trying to lose weight?: ")

def weeks():
    week1 = input("How many lbs did you lose in your first week?: ")
    week2 = input("How many lbs did you lose in your second week?: ")
    week3 = input("How many lbs did you lose in your third week?: ")
    total_weight_lost = (week1)+(week2)+(week3)
    return total_weight_lost,

我可以使用字典或列表吗?像这样:

def weeks():
    d = {"week1": 0, "week2": 0, "week3": 0}
    for k in d:
        total_weight_lost = input(" How much weight did you lose in", d, "?:")
    return total_weight_lost,

我知道这是完全错误的,但我想可能有办法做这样的事情。这样我就可以遍历字典中的Keys并让用户提供值而无需为'week1''week2'和'week3'编写相同的代码行。但不仅仅是我想每个月都重复这个功能,用户说他们试图减肥,然后计算总数。

4 个答案:

答案 0 :(得分:1)

如果您没有使用这些值,则不需要字典。在这种情况下,你可以使用像这样的元组或列表:

def weeks():
    d = ["week1", "week2", "week3"]
    total_weight_lost = 0
    for k in d:
        total_weight_lost += int(input(" How much weight did you lose in", d, "?:"))
    return total_weight_lost,

更简单的可能是这样的:

def weeks():
    total_weight_lost = 0
    for week in xrange(1, 4):
        total_weight_lost += int(input("How much weight did you lose in week {}?".format(week)))
    return total_weight_lost

答案 1 :(得分:1)

考虑到你要问大约三个星期,那么你可以简单地创建一个循环,它会多次询问用户。通过对字符串稍作修改,您可以通过执行以下操作来实现这一点,甚至不需要字典:

def weeks():
    total_weight_lost = 0
    for i in range(1, 4):
        total_weight_lost += int(input("How many lbs did you lose in week {}?: ".format(i)))
    return total_weight_lost

所以我们现在在该方法中所做的是从1开始迭代,并要求用户输入每周的数字。字符串稍有改动,所以现在我们将使用循环迭代器中的每个数字来输出我们需要输入的周数。对于每个条目,我们只保持运行总和。然后返回最终结果。

从上面的解决方案中,可以阅读以下内容:

  1. range
  2. format
  3. 在阅读问题中的评论之后,我想添加一个小答案,以帮助澄清Blckknght所做的comment,它以固定数字作为方法的参数,并会询问用户很多次。

    因此,如果用户表示他们想要输入他们在10周内失去多少重量,那么我们将循环10次以获得参赛作品,我们可以这样做,我改变了方法有点让它给它更多的背景,并采取一个论点。所以weeks()现在可以定义为:

    total_weightloss(weeks):
    

    现在,该方法将采用一个名为weeks的参数,然后我们遍历该数字:

    def total_weightloss(weeks):
        total_weight_lost = 0
        for i in range(1, weeks + 1):
            total_weight_lost += int(input("How many lbs did you lose in week {}?: ".format(i)))
        return total_weight_lost
    

    演示:

    How many lbs did you lose in week 1?: 1
    How many lbs did you lose in week 2?: 2
    How many lbs did you lose in week 3?: 2
    How many lbs did you lose in week 4?: 2
    How many lbs did you lose in week 5?: 1
    How many lbs did you lose in week 6?: 2
    How many lbs did you lose in week 7?: 1
    How many lbs did you lose in week 8?: 1
    How many lbs did you lose in week 9?: 2
    How many lbs did you lose in week 10?: 1
    15
    

    您可以为此做出的下一个修改是错误处理。您可以查看如何修改此方法以确保它只接受整数,以及如果它采用非整数值将会发生什么。请查看exception handling,了解如何处理可能出现的这些错误。

答案 2 :(得分:0)

def weeks():
    d = {"week1": 0, "week2": 0, "week3": 0}
    for i in sorted(d.keys()): 
        total_weight_lost = input(" How much weight did you lose in"+ i+ "?:")
        d[i] = total_weight_lost
    return d

你可以使用它..

print sum(weeks.values())

答案 3 :(得分:0)

如果你需要使用字典来跟踪每周的重量(也许你以后需要这些值来执行其他一些计算)你可以做这样的事情

weeks = {"week1": 0, "week2": 0, "week3": 0}

for key in weeks:
    total_weight_lost = input(" How much weight did you lose in {}".format(key))
    weeks[key] = float(total_weight_lost)

#total weight lost over all weeks
print sum(weeks.values())

但是,如果您只需要总重量的总和,那么您可以使用其他答案之一。