Home > front end >  Iterating only the first element of a list in the inner loop, then letting outer loop do its job. Th
Iterating only the first element of a list in the inner loop, then letting outer loop do its job. Th

Time:10-15

I'm trying to make a simple training weight calculator. First a user inputs a list of exercises, then a list of training maxes. Each gets appended to a list. Then I tried nesting them in two "for" loops with the goal of getting output that is structured as:
"Exercise"
"Training weights for 4 weeks".
Code is as follows:

.
.
.
for tm in training_max:
    for count, e in enumerate(exercises, 1):
        print(count, ') ', e, sep='')
    print('Week 1 -',' ', round(0.65 * float(tm), 2), 'x5,', round(0.75 * float(tm), 2), 'x5,', round(0.85 * float(tm), 2), 'x5 ', sep='')
    print('Week 2 -',' ', round(0.70 * float(tm), 2), 'x3,', round(0.80 * float(tm), 2), 'x3,', round(0.90 * float(tm), 2), 'x3 ', sep='')
    print('Week 3 -',' ', round(0.75 * float(tm), 2), 'x5,', round(0.85 * float(tm), 2), 'x3,', round(0.95 * float(tm), 2), 'x1 ', sep='')
    print('Week 4 -',' ', round(0.40 * float(tm), 2), 'x5,', round(0.50 * float(tm), 2), 'x5,', round(0.60 * float(tm), 2), 'x5' , sep='')

Output is:

Enter the exercises you want to calculate your training max for, one per line!
Enter "done" (without the quotes) to end the list.
Exercise: exercise1
Exercise: exercise2
Exercise: done

['Exercise1', 'Exercise2']

Enter your training maxes in the same order as your exercises (check list above), one per line!
Enter "done" (without the quotes) to end the list.
Training max: 100
Training max: 200
Training max: done

['Exercise1', 'Exercise2']
['100', '200']
1) Exercise1
2) Exercise2
Week 1 - 65.0x5,75.0x5,85.0x5
Week 2 - 70.0x3,80.0x3,90.0x3
Week 3 - 75.0x5,85.0x3,95.0x1
Week 4 - 40.0x5,50.0x5,60.0x5
1) Exercise1
2) Exercise2
Week 1 - 130.0x5,150.0x5,170.0x5
Week 2 - 140.0x3,160.0x3,180.0x3
Week 3 - 150.0x5,170.0x3,190.0x1
Week 4 - 80.0x5,100.0x5,120.0x5

I clearly dont know how to structure the inner loop to print the first element of the exercise list, then have the outer one print out the weights for the first element in the training max list and so on, but that is my goal. Any help would be appreciated!

CodePudding user response:

Found a solution using the zip() function:

for e, tm in zip(exercises, training_max):
    print(e)
    print('Week 1 -', ' ', round(0.65 * float(tm), 2), 'x5,', round(0.75 * float(tm), 2), 'x5,',round(0.85 * float(tm), 2), 'x5 ', sep='')
    print('Week 2 -', ' ', round(0.70 * float(tm), 2), 'x3,', round(0.80 * float(tm), 2), 'x3,',round(0.90 * float(tm), 2), 'x3 ', sep='')
    print('Week 3 -', ' ', round(0.75 * float(tm), 2), 'x5,', round(0.85 * float(tm), 2), 'x3,',round(0.95 * float(tm), 2), 'x1 ', sep='')
    print('Week 4 -', ' ', round(0.40 * float(tm), 2), 'x5,', round(0.50 * float(tm), 2), 'x5,',round(0.60 * float(tm), 2), 'x5', sep='' )
    print('')
  • Related