2013-09-27 43 views
0

好吧,我想做一個簡單的程序來測試我學習的東西有多好,我已經到了一個點,它變得非常大,因爲我想程序將數據存儲在部分(Day1,Day2 .... ect),所以我試圖分配它讀取當前日計數(Num_Days =),但它似乎並不喜歡這樣。我做了一個小測試循環,試着測試一下,如果我能做到這一點,即使邏輯看起來不錯,我也會陷入困境。我試圖做一些搜索,但因爲我不知道我想做甚麼叫我沒有得到很遠。我想要做的就是讓循環讀取Num_Days並給Days()計數並通過'n'將它分配給那一天。簡單的Python循環計數器問題

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天,所以我把它分成兩部分,而不是6天。任何建議是值得歡迎的。

+0

好吧,代碼採樣器似乎不喜歡我,對不起。請按照dpaste – Aoxx

+0

刪除第一行開始處的''''字符(我會這樣做,但我不能進行小於6個字符的編輯) – fjarri

+0

您的理解是,第(1)日'會改變變量'Day1'?如果是這樣,不,請閱讀數組。 – Malvolio

回答

2

歡迎到Python!

Python的美麗之處在於,絕大多數Python程序員儘可能以最「蟒蛇」的方式做事情,因此我要採取的第一步是重命名您的變量,並根據PEP-8 standards。換句話說,類名是大寫的camelcase,但標準變量應該是小寫的下​​劃線分隔。第二,如果你發現自己命名變量的數字如day1, day2, day3等,請停下自己,並意識到如果你不得不延長你的程序的工作時間(100天,或者1,000或10,000),這將是不可維護的。點)。相反,您可以使用名爲days的列表,並根據某些配置變量(如total_days)添加儘可能多的列表。例如:

total_days = 3 
days = [] 
for _ in range(total_days): 
    days.append(0) 

或者,使用list comprehension是更Python:

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真棒?)

我希望我明白你想要做的正確,這可以幫助你。如果您有任何問題,請告訴我。

+0

你也可以這樣做:'days = [0] * 3'。 – Keith

+0

是的,Python很棒,我在工作中使用JCL(Mainframe),我討厭很多這些語言*咳嗽* COBOL *咳嗽*。但是我覺得我的Python很好,只是因爲我不能足夠快地學習我想做的事情。 – Aoxx

+0

順便說一句我發佈了實際工作的代碼(當然減少了這個嘗試的功能),所以你可以得到大局。 – Aoxx

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] 
+0

我可以看到我的問題是如何與這個短片混淆。我有一個程序,試圖跟蹤每天輸出的鍛鍊(使用我自己做的和跟蹤的東西),所以我將它設置爲第1天 - 第6天期間(從此開始,並計劃一旦我進一步將它移動到每週一次),每天需要跟蹤工作總量。並在接下來的一週預計與前一週相比有所增加。但現在我不得不重複每一天的代碼,並且每個代碼都變得越來越大。 – Aoxx

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] 
+0

我在想,當我回到家時,在我的實際代碼前,我會用更多的直接方法來重做我的問題,我不認爲我已經問過這個問題,但我明白那個小節不起作用,我不知道如果一個數組將按照我想要的方式跟蹤事物。 – Aoxx