While Loops Challenges - Problem solver - Stretch and Challenge

Challenge 1: Internet Connection Retry Simulation with Connection Time Analysis

Objective: Write a Python program that simulates the process of setting a connection key to a router and checking for an internet connection. The program will continue attempting to connect until a successful connection is made. It will also track the time taken for the connection and categorise the connection speed as "fast", "medium", or "slow".

Problem Description:

Your task is to simulate connecting to the internet using a router. The process will involve:

  • Setting a connection key (password) to the router.
  • Repeatedly attempting to connect to the internet using a while loop. The connection attempt will randomly succeed or fail.
  • Using the time module to measure the duration of the connection process and determine the connection speed (e.g., "fast", "medium", or "slow").

Requirements:

  1. Set the Router Key:

    • At the start of the program, prompt the user to enter a connection key (this can be any string representing the router password).
  2. Simulate Connection Attempts:

    • Use a while loop that continues to run until a successful connection is made.
    • Simulate the internet connection attempt using a boolean random value, where True represents a successful connection and False represents a failure.
  3. Track the Time Taken:

    • Use the time module to record the time at the start of the loop and once a successful connection is made. Calculate the total time taken to connect.
  4. Classify the Connection Time:

    • Classify the connection time as:
      • "Fast": If the connection time is less than 2 seconds.
      • "Medium": If the connection time is between 2 and 5 seconds.
      • "Slow": If the connection time exceeds 5 seconds.
  5. Print the Connection Results:

    • Once a successful connection is made, print the total time taken for the connection and whether it was "fast", "medium", or "slow".

Hints:

  • Use the random module to randomly simulate successful or failed connection attempts (random.choice([True, False])).
  • Use the time.time() function to track the start and end times of the connection attempt.
  • Use if statements to classify the connection time.


Challenge 2: Netflix login simulation - using lists

You are tasked with creating a Netflix login simulation. A user will first log in by providing their username and age, and then they can choose either a movie or TV show to watch. Content is categorized into three age rating lists: "All", "13+", and "18+". The user can only access content that is appropriate for their age.

Program Flow:

  1. Login (Enter Username and Age):

    • The program prompts the user to enter a username and age.
    • The username will be displayed during the movie/TV show selection process.
  2. Display Available Content:

    • Movies and TV shows will be grouped into three separate lists:
      • "All": Content available to everyone.
      • "13+": Content available to users aged 13 and older.
      • "18+": Content available to users aged 18 and older.
  3. Check Age Restrictions:

    • After the user selects a movie or show, the program checks if they are allowed to watch it based on their age.
    • If they choose content that is age-restricted and they are not old enough, the program will prompt them to choose again.
  4. While Loop for Input Validation:

    • The program uses a while loop to keep asking the user for input until they select a movie or show that they are allowed to watch.

Key Concepts to Practice:

  • Input handling for usernames and age verification.
  • Using while loops for continuous input until valid conditions are met.
  • Using lists to categorise items and conditionally checking age restrictions.