Question

OBJECTIVES: To understand repetition structures To use while loops in a program To understand break and/or...

OBJECTIVES:

  1. To understand repetition structures
  2. To use while loops in a program
  3. To understand break and/or continue instructions
  1. Write a Python program (with comments) to do the following:
    1. Create a variable count and initialize it to 0
    2. Create a variable colors and initialize it to [] (i.e. an empty list)
    3. Ask the user if they would like to enter up to 3 favorite colors. They should enter ‘y’ or ‘n’. Assume they will enter a valid input. Save the response to a variable flag.
    4. while flag is not ‘n’ and count is less than 3, do the following inside a loop:
      1. Ask the user to enter one of their favorite colors and append it to the list colors. Assume they will enter a valid input.
      2. Increment count by 1.
      3. Ask the user if they want to continue They should enter ‘y’ or ‘n’. Assume they will enter a valid input. Save the response and in flag.
    5. After exiting the loop print i) how many colors the user entered and ii) the list of colors entered by the user, if they entered at least 1 color.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

PYTHON CODE:

# Counter to count how many colors user has entered
count = 0
# List of colors
colors = []
# Ask user if he wants to enter colors or not
flag = input('Do you want to enter up to 3 favorite colors? (y/n): ')
# Loop till user denies or has entered more than 3 colors
while flag is not 'n' and count < 3:
   # Ask user to enter his favorite color
   color = input('Enter your favorite color: ')
   # Append color to the list of colors
   colors.append(color)
   # Increment counter
   count += 1
   # Ask user if he wants to continue entering colors
   flag = input('Do you want to continue? (y/n): ')

# Print number of colors user has entered
print('The user entered %d colors' % (count))
# If user has entered any color then only print the list of colors
if count > 0:
   print('List of colors:', colors)

SAMPLE OUTPUT:

Add a comment
Know the answer?
Add Answer to:
OBJECTIVES: To understand repetition structures To use while loops in a program To understand break and/or...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Create a new Python program in IDLE, and save it as lab8.py.(Again, I recommend saving lab...

    Create a new Python program in IDLE, and save it as lab8.py.(Again, I recommend saving lab progranms Your Python program will do the following: Create an empty list . Use a for loop to ask the user for 10 numbers. Add each number to your list using the append metha . Use another for loop to print the list in reverse order, one per line . Use a while loop to count how many positive numbers are in the list...

  • Write a program that incorporates these functions and statements: • list • while loop • for...

    Write a program that incorporates these functions and statements: • list • while loop • for loop • if • print() • input() • concatenation This program must create a grocery shopping list based on the user input • the program will ask a user to enter a new grocery item until the user presses Enter • then all user input will be added to a list • then the list with all grocery items will be printed

  • #include<iostream> using namespace std; int main() { float num,avg,sum=0.0; int count=0; bool moreNumbers=true; cout<<"Enter grades:"<<endl; while(moreNumbers)...

    #include<iostream> using namespace std; int main() { float num,avg,sum=0.0; int count=0; bool moreNumbers=true; cout<<"Enter grades:"<<endl; while(moreNumbers) { cin>>num; if(num < 0) { moreNumbers=false; } else { count = count+1; sum=sum+num; } } avg=sum/count; cout<<"Average grade:"<<avg<<endl; cout<<"How many grades were entered:"<<count<<endl; return 0; } Question: We need to add another loop to check input. Just like the input loop we already have, this one should use a boolean variable as a control. So, the logic could be something like this: Loop...

  • Factorial.java This is an exercise in using repetition structures to calculate the mathematical values of the...

    Factorial.java This is an exercise in using repetition structures to calculate the mathematical values of the factorial function. Since methods have not yet been covered in detail, you may do the calculations in a class that contains just a main method. The looping structures require the use of count-controlled loops. This program computes the factorial function of a non-negative integer, which is written mathematically as n! (the parameter n followed by an exclamation mark). For values of n equal to...

  • Lab 5-2 Nested Loops 2. Summation Of Numbers (using Nested While Loops) Part A: The program...

    Lab 5-2 Nested Loops 2. Summation Of Numbers (using Nested While Loops) Part A: The program will calculate and display the total of all numbers up to a specific number (entered by the user). Only positive numbers are allowed. Part B: User-controlled loop Part A Input Validation loop Part A: Summation loop Examples: When 5 is entered the program will calculate the total as 1+2+...+5 and display 15. When 2 is enterered the program will calculate the total as 1+2...

  • Q1 (2 pts) Shopping list Write a program that prompts a user for items on their...

    Q1 (2 pts) Shopping list Write a program that prompts a user for items on their shopping list and keeps prompting the user until they enter "Done" (make sure your program can stop even if the user enters "Done" with different cases, like "done"), then prints out the items in the list, line by line, and prints the total number of items on the shopping list. Output (after collecting items in the while loop) should look something like this: Apple...

  • // Group Names: // Date: // Program Description: //   Import required packages //--> //   Declare class...

    // Group Names: // Date: // Program Description: //   Import required packages //--> //   Declare class (SwitchDoLab) //--> {    //   Declare the main method    //-->    {        //   Declare Constant integers SUM = 1, FACTORIAL = 2, QUIT = 3.        //-->        //-->        //-->        //   Create an integer variable named choice to store user's option.        //-->        //   Create a Scanner object        //   Create...

  • In Python Exercise – For & While Loops Create a new file called loops.py and use...

    In Python Exercise – For & While Loops Create a new file called loops.py and use it for all parts of this exercise. Remember the difference between input and raw input? Be sure to test your code for each part before moving on to the next part. Write a program using a for loop that calculates exponentials. Your program should ask the user for a base base and an exponent exp, and calculate baseexp. Write a program using a while...

  • Program Requirements ·         The program prompts the user to enter a loan amount and an interest rate....

    Program Requirements ·         The program prompts the user to enter a loan amount and an interest rate. ·         The application calculates the interest amount and formats the loan amount, interest rate, and interest amount. Then, it displays the formatted results to the user. ·         The application prompts the user to continue. ·         The program stops prompting the user for values after taking 3 loan amounts. ·         The application calculates and displays the total loan and interest amount and ends the program Example Output Welcome to...

  • Topics c++ Do While loops cin.ignore Description Redo the last assignment with the following modifications: 1....

    Topics c++ Do While loops cin.ignore Description Redo the last assignment with the following modifications: 1. When the program asks the user to enter a student name, the user will enter the full name (e.g. John Smith). Make changes to the program to accommodate that. 2. When the program asks the user to enter the test score, the user will enter the test score as before. However, make changes to the program so that it will check that the score...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT