好的,我正在尝试制作一个简单的程序,以便有点测试我学习的东西,我已经达到了一个非常大的程度,因为我希望程序将数据存储在各个部分(Day1,Day2。 ...等等)所以我试着指定它来读取当前的日数(Num_Days =),但它似乎不喜欢这样。我做了一个小测试循环,尝试测试我是否可以做到这一点,并且即使逻辑看起来对我来说已经卡住了。我尝试做一些搜索,但因为我不知道我想要做什么甚至被称为我还没走得很远。我想要做的是让循环读取Num_Days并给Days()计数并通过'n'将其分配到那一天。
Num_Days = 0
Total = 0
Data = 0
Day1 = 0
Day2 = 0
Day3 = 0
def Start_Work(x):
while Num_Days < 3:
Num_Days += 1
print "This is Day:",Num_Days
n = Num_Days
Total = +20
Day(n) += Total
else:
print "failed"
x = str('start')
我也对它做了一个dpaste,因为我更容易用那种方式看待它然后是全黑:http://dpaste.com/1398446/
为了清楚地弄清楚我失去了一些人的想法,我只是想做一个自己的循环,我会提出我想要使用它的东西。这个程序按照我想要的方式运行,问题在于如果我想让它变得更大它会变得很长。
NumDays = 0
TotalOut = 0
Day1Tot = 0
Day1_RepsCnt = 0
Day4 = 0
def Work_Out(x):
while x == 1: ##crunches
NumDays = 0
TotalOut = 0
Day1Tot = 0
Day1_RepsCnt = 0
Day1_WghtCnt = 0
Day4 = 0
while NumDays < 3:
Day1_Wght = float(raw_input("How much weight did you use?"))
Day1_Reps = float(raw_input("How many reps did you do?"))
Day1_Sets = float(raw_input("How many sets were done?"))
Day1 = Day1_Wght * Day1_Reps * Day1_Sets
NumDays += 1
print "Day:",NumDays
print "Your total output is:",Day1
Day1_RepsCnt += Day1_Reps
Day1_WghtCnt += Day1_Wght
Day1Tot += Day1
TotalOut += Day1
if NumDays == 3:
print "Your total output for 3 days is:",TotalOut
print "Lets increase the Weight to",(Day1_Wght + 10)
print "Increase the Weight for days 4-6"
while NumDays >= 3 and NumDays <6:
Day4_Wght = float(raw_input("How much weight did you use?"))
if Day4_Wght <= (Day1_WghtCnt/3):
print "You need to increase your total output, add 10 pounds."
break
Day4_Reps = float(raw_input("How many reps did you do?"))
Day4_Sets = float(raw_input("How many sets were done?"))
Day4 += Day4_Wght * Day4_Reps * Day4_Sets
NumDays += 1
print "Day:",NumDays
if Day4_Wght <= (Day1_WghtCnt/3):
print "Re-enter totals once you have added the additional weight."
else :
print "Your total output was:",Day4
while x == 2: ##Benching
NumDays = 0
TotalOut = 0
Day1Tot = 0
Day1_RepsCnt = 0
Day4 = 0
while NumDays < 3:
Day1_Wght = float(raw_input("How much weight did you use?"))
Day1_Reps = float(raw_input("How many reps did you do?"))
Day1_Sets = float(raw_input("How many sets were done?"))
Day1 = Day1_Wght * Day1_Reps * Day1_Sets
NumDays += 1
print "Day:",NumDays
print "Your total output is:",Day1
Day1_RepsCnt += Day1_Reps
Day1Tot += Day1
TotalOut += Day1
if NumDays == 3:
print "Your total output for 3 days is:",TotalOut
print "Lets increase the Reps to",(Day1_Reps + 10)
print "Increase reps for days 4-6"
while NumDays >= 3 and NumDays <6:
Day4_Wght = float(raw_input("How much weight did you use?"))
Day4_Reps = float(raw_input("How many reps did you do?"))
if Day4_Reps <= (Day1_RepsCnt/3):
print "You need to increase your total output, do 10 more Reps."
break
Day4_Sets = float(raw_input("How many sets were done?"))
Day4 += Day4_Wght * Day4_Reps * Day4_Sets
NumDays += 1
print "Day:",NumDays
if Day4_Reps <= (Day1_RepsCnt/3):
print "Re-enter totals once you have completed the additional reps."
else :
print "Your total output was:",Day4
print "Available work outs in this version: crunches, benching"
Input = raw_input("What type of Work Out did you do?")
if Input.lower() == str('crunches'):
Work_Out(1)
if Input.lower() == str('benching'):
Work_Out(2)
else:
print "Failed"
是的,我明白这需要清理,但我还有其他的想法,我想要扔在那里和我想要重新排列的东西,但现在它只是想弄清楚我怎么能把它分解成每周一次的周期,并且每周都会进入每日周期,所以我开始尝试通过一周,并且发现仅仅试图过去2天就很难,所以我把它分成2个部分而不是6天。欢迎任何建议。
答案 0 :(得分:2)
欢迎使用Python!
Python的优点之一是绝大多数Python程序员尽可能以最“Pythonic”的方式做事,所以我要采取的第一步是根据{重命名变量等。 {3}}。换句话说,类名将是大写的camelcase,但标准变量应该是小写下划线分隔。
其次,如果您发现自己使用day1, day2, day3
等数字命名变量,请停止自己并意识到如果您必须将程序扩展到100天(或1,000或10,000)的工作量,那将是多么难以维护,你明白了。)相反,您可以使用名为days
的列表,并根据某些配置变量(例如total_days
)添加任意数量的列表。例如:
total_days = 3
days = []
for _ in range(total_days):
days.append(0)
或者,使用PEP-8 standards更多Pythonic:
total_days = 3
days = [0 for _ in range(total_days)]
使用这些实现,您只需更改total_days
的值即可添加更多天数。考虑到所有这些,让我们尝试重现您的程序:
# method definition
def start_workouts(total_days, init_workout, workout_increase):
workouts = [(init_workout + day * workout_increase) for day in range(total_days)]
return workouts
# method execution (3 days, starting at 100, increasing 20 each day)
my_workouts = start_workouts(3, 100, 20)
# my_workouts == [100, 120, 140]
# my_workouts[0] is "Day1"
# my_workouts[1] is "Day2"
# my_workouts[2] is "Day3"
因此,请注意我们将一些变量声明作为参数传递给您的方法。这样,您可以根据您稍后可能决定的各种情况轻松更改锻炼的标准。此外,我们将所有计算减少为单个列表理解的一部分! (Python不是很棒吗?)
我希望我理解你正在努力做的事情,这会帮助你。如果您有任何问题,请告诉我。
答案 1 :(得分:0)
明确设置Days
可能不是最佳选择,正如@blakev所说,只需使用列表。
Num_Days = 0
Total = 0
Data = 0
Days = []
def Start_Work():
while Num_Days < 3:
Num_Days += 1
print "This is Day:",Num_Days
Total += 20
Days[Num_Days] = Total
else:
print "failed"
Start_Work() # call the function
你应该得到类似
的输出[20, 40, 60]
答案 2 :(得分:0)
您的代码看起来不正确Python
代码。以下是更正:
#-------------------
#while (condition):
# #commands
#-------------------
#for i in xrange(3):
# #commands
#-------------------
#Examples:
Num_Days = 0
Total = 0
Day = [0,0,0]
while Num_Days<3:
Num_Days += 1
print "This is Day:",Num_Days
Total += 20
Day[Num_Days-1] += Total
print Day
>>>
This is Day: 1
This is Day: 2
This is Day: 3
[20, 40, 60]
或更好地使用:
Total = 0
Day = [0,0,0]
n = 3
for i in xrange(n):
print "This is Day:",i+1
Total += 20
Day[i] += Total
print Day
>>>
This is Day: 1
This is Day: 2
This is Day: 3
[20, 40, 60]