【发布时间】:2013-09-28 19:38:09
【问题描述】:
我正在尝试创建一个函数,该函数将启动循环并在当前日期计数中添加一天,它将询问 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"
【问题讨论】:
-
顺便说一句,
Title_Case_With_Underscores怎么了?另外'start'已经是str,无需将其转换为一个。while x == 1又是怎么回事?这个x来自哪里?另外,查找xrange内置函数。 -
这是一个更大的程序的一部分,我计划在几天、几周和几个月内完成这些,所以我想让函数名称非常独特。 'x' 是因为在更大的程序中它会要求进行练习......即:“长凳,仰卧起坐,深蹲”,其中长凳 = 1,仰卧起坐 = 2 和深蹲 = 3。这就是“x”会改变。每个练习都会有循环之外的循环,需要通过重量、次数或组数来增加输出。我实际上是想清除“开始”部分,因为它是从我之前尝试写这个的时候开始的。
-
哦,开始部分是因为我认为我必须声明类型以强制它更低,我会尝试不使用“str”并为自己节省一些键。跨度>
标签: python function python-2.7 tuples