craigs-classroom.com

Basic Python Input and Validation

Task 1: Printing a Name

Objective: Learn how to print a static string in Python.

Open Online Python.

In the editor, type:

print(“Hello, Alice!”)

Click Run and observe the output.

Change the name to your own and run it again.


Task 2: Using a Variable for a Name

Objective: Store a name in a variable and print it.

Modify your code:
name = “Alice”

print(“Hello, ” + name + “!”)

  1. Run the script and check the output.
  2. Change the variable to your own name and run it again.

Task 3: Taking User Input

Objective: Get the user’s name from input.

Modify the script:
name = input(“Enter your name: “)

print(“Hello, ” + name + “!”)

  1. Run the script and enter your name.
  2. Observe how the program responds with a personalised greeting.

Task 4: Validating Name Length

Objective: Ensure the name is not too short or too long.

Modify the script:
name = input(“Enter your name: “)

if 2 <= len(name) <= 20:

    print(“Hello, ” + name + “!”)

else:

print(“Invalid name. It must be between 2 and 20 characters.”)

  • Run the script and enter names of different lengths.
  • Observe how the validation prevents names that are too short or too long.

Task 5: Validating Alphabetic Characters Only

Objective: Ensure the name contains only letters.

Modify the script:
name = input(“Enter your name: “)

if name.isalpha():

    print(“Hello, ” + name + “!”)

else:

print(“Invalid name. Use only letters.”)

  1. Run the script and enter names with numbers or special characters.
  2. Observe how non-alphabetic inputs are rejected.

Task 6: Repeating Input Until It’s Valid

Objective: Keep asking for a valid name if incorrect input is entered.

Modify the script:
while True:

    name = input(“Enter your name: “)

    if name.isalpha():

        print(“Hello, ” + name + “!”)

        break

    else:

print(“Invalid name. Please use only letters.”)

  1. Run the script and try entering invalid names.
  2. Observe how the program keeps asking until valid input is provided.

Task 7: Asking for Age (Introducing Integers)

Objective: Collect numeric input and ensure it is a number.

Modify the script:
age = input(“Enter your age: “)

if age.isdigit():

    print(“You are ” + age + ” years old.”)

else:

print(“Invalid age. Please enter numbers only.”)

  1. Run the script and try entering text instead of a number.
  2. Observe how non-numeric inputs are rejected.

Task 8: Repeating Input Until a Valid Age is Entered

Objective: Ensure only valid ages are accepted.

Modify the script:
while True:

    age = input(“Enter your age: “)

    if age.isdigit():

        print(“You are ” + age + ” years old.”)

        break

    else:

print(“Invalid input. Please enter a number.”)

  1. Run the script and try entering invalid ages.
  2. Observe how the loop ensures only valid numbers are accepted.

Task 9: Checking Eligibility for Riding a Moped

Objective: Introduce conditional statements based on age requirements.

Modify the script:
while True:

    age = input(“Enter your age: “)

    if age.isdigit():

        age = int(age)

        print(“You are ” + str(age) + ” years old.”)

        break

    else:

        print(“Invalid input. Please enter a number.”)

if age >= 16:

    print(“You are old enough to ride a moped!”)

else:

print(“Sorry, you must be at least 16 to ride a moped.”)

  1. Run the script and enter different ages to test eligibility.
  2. Observe how the program determines if the user can ride a moped.

Task 10: Checking Eligibility for Cars and HGVs

Objective: Expand conditional logic to include cars and HGVs.

Modify the script:
if age >= 21:

    print(“You are old enough to drive a Heavy Goods Vehicle (HGV).”)

elif age >= 17:

    print(“You are old enough to drive a car.”)

elif age >= 16:

    print(“You are old enough to ride a moped.”)

else:

print(“Sorry, you must be at least 16 to ride a moped.”)

  1. Run the script and enter different ages to test eligibility.
  2. Observe how the program determines the correct vehicle permissions based on age.

2023