Question

In: Computer Science

Create Python Code using a "for" loop and a "while" loop. You are starting a twelve...

Create Python Code using a "for" loop and a "while" loop. You are starting a twelve week program training to compete in a triathlon. The triathlon consists of three athletic events, 1.5 k swim, 40k bike, 10k run. In order to be prepared for the competition you want to print a training schedule. Starting with week 1 you will increase the distance of each activity so that you reach the race distance by week twelve. Due to rounding, you may be just under or over, and that’s okay. Display output to 2 decimal places. Since you want your program to be flexible, the program should the prompt for the athlete’s name and current level of each activity. Also allow the user to continue to enter training information for athletes until they say ‘no’ to the question “do you want to enter another”.

Solutions

Expert Solution

# Loops till user choice is not 'n' or 'N'
while True:
# Accepts name
name = input("Enster athlete’s name: ")
# Accepts type of athletic
athleticEvent = input("Enter athletic event (swim / bike / run): ")

# Checks if type of even is 'swim'
if (athleticEvent == 'swim'):
# Converts to meter and calculates first week value
required = (1.5 * 100) / 12

# Otherwise checks if type of even is 'bike'
elif(athleticEvent == 'bike'):
# Converts to meter and calculates first week value
required = (40 * 100) / 12

# Checks if type of even is 'run'
elif(athleticEvent == 'run'):
# Converts to meter and calculates first week value
required = (10 * 100) / 12
# Stores the first week distance
distance = required

# Loops 12 times for 12 weeks
for c in range(1, 13):
  
# Displays week number
print('Week ', c, ' distance to cover: ', end = ' ')
# Displays distance with 2 decimal places in meter
print("%.2f" % round(distance, 2), 'meter')

# Calculate next week distance
distance += required

# After 12 week accept user choice to continue or not
ch = input('Would you like to continue(y/n)? ')

# Checks if choice is 'n' or 'N'
if(ch == 'n' or ch == 'N'):
# Stop the loop
break
  

Sample Output:

Enster athlete’s name: Mohan
Enter athletic event (swim / bike / run): swim
Week 1 distance to cover: 12.50 meter
Week 2 distance to cover: 25.00 meter
Week 3 distance to cover: 37.50 meter
Week 4 distance to cover: 50.00 meter
Week 5 distance to cover: 62.50 meter
Week 6 distance to cover: 75.00 meter
Week 7 distance to cover: 87.50 meter
Week 8 distance to cover: 100.00 meter
Week 9 distance to cover: 112.50 meter
Week 10 distance to cover: 125.00 meter
Week 11 distance to cover: 137.50 meter
Week 12 distance to cover: 150.00 meter
Would you like to continue(y/n)? y
Enster athlete’s name: Pyari
Enter athletic event (swim / bike / run): run
Week 1 distance to cover: 83.33 meter
Week 2 distance to cover: 166.67 meter
Week 3 distance to cover: 250.00 meter
Week 4 distance to cover: 333.33 meter
Week 5 distance to cover: 416.67 meter
Week 6 distance to cover: 500.00 meter
Week 7 distance to cover: 583.33 meter
Week 8 distance to cover: 666.67 meter
Week 9 distance to cover: 750.00 meter
Week 10 distance to cover: 833.33 meter
Week 11 distance to cover: 916.67 meter
Week 12 distance to cover: 1000.00 meter
Would you like to continue(y/n)? y
Enster athlete’s name: Manvi
Enter athletic event (swim / bike / run): bike
Week 1 distance to cover: 333.33 meter
Week 2 distance to cover: 666.67 meter
Week 3 distance to cover: 1000.00 meter
Week 4 distance to cover: 1333.33 meter
Week 5 distance to cover: 1666.67 meter
Week 6 distance to cover: 2000.00 meter
Week 7 distance to cover: 2333.33 meter
Week 8 distance to cover: 2666.67 meter
Week 9 distance to cover: 3000.00 meter
Week 10 distance to cover: 3333.33 meter
Week 11 distance to cover: 3666.67 meter
Week 12 distance to cover: 4000.00 meter
Would you like to continue(y/n)? n


Related Solutions

Create a python code that calculates fixed point iteration method using a for loop.
Create a python code that calculates fixed point iteration method using a for loop.
Important: please use python. Using while loop, write python code to print the times table (from...
Important: please use python. Using while loop, write python code to print the times table (from 0 to 20, incremented by 2) for number 5. Add asterisks (****) so the output looks exactly as shown below.   Please send the code and the output of the program. ****************************************************************** This Program Shows Times Table for Number 5 (from 0 to 20) Incremented by 2 * ****************************************************************** 0 x 5 = 0 2 x 5 = 10 4 x 5 = 20 6...
Can you rewrite this MATLAB code using a for loop instead of a while loop? %formatting...
Can you rewrite this MATLAB code using a for loop instead of a while loop? %formatting clc, clear, format compact; %define variables k=1; b=-2; x=-1; y=-2; %while loop initialization for k <= 3 disp([num2str(k), ' ',num2str(b),' ',num2str(x),' ',num2str(y),]); y = x^2 -3; if y< b b = y; end x = x+1; k = k+1; end
Code in python Write a while loop code where it always starts form 2. Then it...
Code in python Write a while loop code where it always starts form 2. Then it randomly chooses a number from 1-4. If the number 4 is hit then it will write “TP” if the number 1 is hit then it will write”SL”. It will rerun the program every time the numbers 1 and 5 are hit. The code should also output every single number that is randomly chosen. 2 of the same numbers can't be chosen back to back...
Write a python code which prints triangle of stars using a loop ( for loop )...
Write a python code which prints triangle of stars using a loop ( for loop ) Remember what 5 * "*" does The number of lines of output should be determined by the user. For example, if the user enters 3, your output should be: * ** *** If the user enters 6, the output should be: * ** *** **** ***** ****** You do NOT need to check for valid input in this program. You may assume the user...
Create a python program that contains a while loop together with a Sentinel (0) to process...
Create a python program that contains a while loop together with a Sentinel (0) to process indefinite item costs that are purchased online from a vendor. Be sure that you assign a variable SENTINEL to 0 to use in the Boolean condition of your while loop. A sales tax rate of 6.25% is applied to the subtotal for the items purchased. Be sure you assign a variable, TAXRATE to 0.0625. The program is to process a number of items, numItems,...
Write a program in PYTHON, using a while loop, that asks the user to enter the...
Write a program in PYTHON, using a while loop, that asks the user to enter the amount that they have budgeted for the month. The program should then prompt the user to enter their expenses for the month. The program should keep a running total. Once the user has finished entering their expenses the program should then display if the user is over or under budget. The output should display the monthly budget, the total expenses and whether the user...
```please convert this code to make only using for loop not while loop #include #include #define...
```please convert this code to make only using for loop not while loop #include #include #define MAX_SIZE 500 int main() { char str[MAX_SIZE]; char tosearch[MAX_SIZE]; char part1[100]; char part2[100]; int cursor = 0, i, cnt1 = 0, cnt2 = 0, cnt = 0; int j = 0; int a = 0; int b = 0; int total = 0; printf("Enter any string: "); gets(str); printf("Enter word to search occurrences: "); gets(tosearch); for (i = 0; i < strlen(tosearch); i++) {...
```please convert this code to make only using for loop not while loop #include #include #define...
```please convert this code to make only using for loop not while loop #include #include #define MAX_SIZE 500 int main() { char str[MAX_SIZE]; char tosearch[MAX_SIZE]; char part1[100]; char part2[100]; int cursor = 0, i, cnt1 = 0, cnt2 = 0, cnt = 0; int j = 0; int a = 0; int b = 0; int total = 0; printf("Enter any string: "); gets(str); printf("Enter word to search occurrences: "); gets(tosearch); for (i = 0; i < strlen(tosearch); i++) {...
Using Python code create a program only with beginners code. You are taking online reservations at...
Using Python code create a program only with beginners code. You are taking online reservations at the The inn Ask for your client’s name and save in a variable Ask how many nights your client will be staying and save in a variable Room rental is $145 per night Sales tax is 8.5% Habitation tax is $5 per night (not subject to sales tax) Print out: Client’s name Room rate per night Number of nights Room cost (room rate *...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT