编写以前的数据的python元组

时间:2013-09-28 19:38:09

标签: python function python-2.7 tuples

我正在尝试创建一个函数,它将启动循环并为当天计数添加一天,它将询问3个问题,然后将该数据组合为等于Total_Output。然后我想要'n'来表示元组的结尾,并在下一步中将Total_Output添加到元组的结尾。但是当我运行该函数时,它似乎正在创建一个新的元组。

示例:

Good Morninghi
This is Day: 1
How much weight did you use?40
How many reps did you do?20
How many sets did you do?6
Day: 1
[4800.0]
This is Day: 2
How much weight did you use?50
How many reps did you do?20
How many sets did you do?6
Day: 2
[6000.0, 6000.0]
This is Day: 3
How much weight did you use?40
How many reps did you do?20
How many sets did you do?6
Day: 3
[4800.0, 4800.0, 4800.0]
failed

这是功能:

def Start_Work(x):
    Num_Days = 0
    Total_Output = 0
    Wght = 0
    Reps = 0
    Sets = 0
    Day = []

    while x == 1 and Num_Days < 6: ##will be doing in cycles of 6 days
        Num_Days += 1     ##increase day count with each loop
        print "This is Day:",Num_Days
        Wght = float(raw_input("How much weight did you use?"))
        Reps = float(raw_input("How many reps did you do?"))
        Sets = float(raw_input("How many sets did you do?"))
        Total_Output = Wght * Reps * Sets
        n = Day[:-1]   ##go to end of tuple
        Day = [Total_Output for n in range(Num_Days)] ##add data (Total_Output to end of tuple
        print "Day:",Num_Days  
        print Day
    else:
        print "failed"

Input = raw_input("Good Morning")
if Input.lower() == str('hi') or str('start') or str('good morning'):
  Start_Work(1)
else:
    print "Good Bye"

1 个答案:

答案 0 :(得分:1)

n = Day[:-1]   ##go to end of tuple
Day = [Total_Output for n in range(Num_Days)] ##add data (Total_Output to end of tuple

不按照您的想法行事。您分配n但从不使用它(循环中的nfor n in分配),并且只保留{{1}的末尾list变量。

然后,您将Day设置为Day,这样就可以[Total_Output] * Num_Days list Num_Days次出现Total_Output

你想:

Day.append(Total_Output)

替换这两行。