Practical Challenges 2

<< Previous: Python mathsNext: Selection >>

About the challenges

The challenges below allow you to apply your knowledge of the topics up to this point. Attempt the challenges in a Python IDE of your choice.

Challenge 1: Username

Ask the user to input their forename and surname separately and force both tobe uppercase. Generate a username that is the last three letters of their surname followed by the first three letters of their first name e.g. John Smith would generate the username: ITHJOH



forename = input("Enter your forename: ").upper()
surname = input("Enter your surname: ").upper()
username = surname[-3:] + forename[:3]
print(f"Your username is {username}")

Challenge 2: Average calculator

Write a program that will ask the user to input two numbers and then calculate the average of the two numbers e.g. if the user enters 6 and 8 the average will be 7.

HINT: Add two numbers and divide by two to find the average of the two numbers.



num1 = int(input("Enter the first number: "))
num2 = int(input("Enter the second number: "))
average = (num1 + num2)/2
print(f"The average of {num1} and {num2} is {average}.")

Challenge 3: Remainder calculator

Write a program that will ask the user to input two numbers and divide the first by the second but giving a whole number and remainder e.g. if the user enters 9 and 4 the output will be 9/4 = 2 remainder 1.

HINT: Use modulus and floor division.



num1 = int(input("Enter the first number: "))
num2 = int(input("Enter the second number: "))
whole = num1 // num2
part = num1 % num2
print(f"{num1} / {num2} is {whole} remainder {part}.")

Challenge 4: Taxi fare

Write a program that will ask the user to input the number of passengers and distance a taxi travels and output the cost of the journey. Journey costs should be calculated as £2 per passenger plus £1.50 per mile.



num_passengers = int(input("Enter the number of passengers: "))
num_miles = int(input("Enter the number of miles travelled: "))
cost = num_passengers * 2 + num_miles * 1.50
print(f"The cost of a journey with {num_passengers} travelling {num_miles} is £{cost}.")
<< Previous: Python mathsNext: Selection >>

© All materials created by and copyright S.Goff