0

Ok, I am trying to make a simple program to kinda test how well i am learning things, I have come to a point where it is getting very large as I want the program to store data in sections (Day1,Day2....ect) so i tried to assign it to read the current Day count (Num_Days = ) but it doesnt seem to like this. I made a small test loop to try and test out if i could do this and have gotten stuck even though the logic looks good to me. I tried to do some searches but as i dont know what I am trying to do is even called I havent gotten very far. What I want to do is have the loop read the Num_Days and give the Days() the count and assign it to that day through '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')

I also made a dpaste on it as it is easier for me to look at it that way then in the full black: http://dpaste.com/1398446/

In order to clear up apparently where I lost some people with thinking that I am just trying to make a single loop that sits by its self I am going to put up what I am trying to use this for. This program is functioning the way I have wanted it to, the problem being that if i wanted o make it bigger it would get to be very long.

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"

And yes I understand that this needs to be cleaned up, but I have other ideas of what i want to throw in there and things i want to rearrange, but right now its just trying to figure out how I can break this into weekly cycles, and break each week into daily cycles, so i started with trying to get through one week and figure out that it would be very difficult just trying to get past 2 days so i broke it into 2 parts instead of 6 days. Any advise is welcome.

4

3 回答 3

2

欢迎来到 Python!

Python 的优点之一是绝大多数 Python 程序员尽可能以最“Pythonic”的方式做事,所以我要采取的第一步是根据PEP-8 标准重命名变量等. 换句话说,类名将大写驼峰式,但标准变量应小写下划线分隔。

其次,如果您发现自己用诸如此类的数字命名变量day1, day2, day3,请停下来并意识到如果您不得不将程序扩展为使用 100 天(或 1,000 或 10,000 天,您明白了),那将是多么难以维护。相反,您可以使用一个名为的列表days,并根据一些配置变量(例如total_days)添加任意数量的列表。例如:

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

或者,使用列表推导更 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 不是很棒吗?)

我希望我理解你试图正确做的事情,这可以帮助你。如果您有任何问题,请告诉我。

于 2013-09-28T00:18:10.410 回答
0

正如@blakev 所说,明确设置可能不是最好的主意Days,只需使用列表即可。

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]
于 2013-09-27T23:47:51.943 回答
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]
于 2013-09-27T23:59:02.520 回答