CompSci
  • Home
  •  Unit 1
    Output Exercises
     Unit 2
    Variables Math Modules Exercises
     Unit 3
    Conditionals Exercises
     Unit 4
    Iteration / Loops Exercises
     Unit 5
    Strings Exercises
     Unit 6
    Lists Exercises
     Unit 7
    Functions Exercises
  •  Unit 1
    Java Introduction Java Syntax Errors & Coding Conventions Format Method Exercises
     Unit 2
    Variable Introduction Math / Arithmetic Operators Obtaining a Value Formatting Numeric Output Concatenation & Constants Division & Type Casting Exercises
     Unit 3
    Conditionals Exercises
     Unit 4
    while Loops for Loops Exercises
     Unit 5
    Strings Exercises
     Unit 6
    Methods Method Parameters Method Return Types Exercises
  • Muddy City
  • Github Classroom
  •  Assignments
    Add an Assignment Edit Assignment Search Program Code
     Forum
    Ask a Question Search the Forum
     New Account
    New Account


Account Mgmt
Reset Password

Python - Conditionals


Exercises

Equal To, Greater Than, or Less Than

Difficulty: Low

Title: Equal To, Greater Than, or Less Than

Description:
Write a program to accept two integers and check whether they are equal to, greater than, or less than each other.

  • NOTE: The output needs to match the following structure: number 1 is greater / less / equal to / than number 2

Output:
Type a Number: 5
Type a Number: 6

5 is less than 6


Type a Number: 5
Type a Number: 5

5 is equal to 5

Type a Number: 8
Type a Number: 7

8 is greater than 7


Weather

Difficulty: Low

Title: Weather

Description:
Write a program to prompt a user for a temperature in Fahrenheit and display a suitable message according to the temperature state below:

  • Temp < 32: Freezing Weather
  • Temp 32 - 45: Very Cold Weather
  • Temp 46 - 60: Cold Weather
  • Temp 61 - 79: Moderate Weather
  • Temp 80 - 89: Hot Weather
  • Temp >= 90: Very Hot

Output:
Enter the temperature: 47

This is: Cold Weather


Enter the temperature: 92

This is: Very Hot

Enter the temperature: 75

This is: Moderate Weather


Largest of Three Numbers

Difficulty: Low

Title: Largest of Three Numbers

Description:
Write a program to find the largest of three numbers. The numbers can be typed in any order (i.e. smallest to largest, largest to smallest, or the largest number in the middle).

  • Prompt the user for three numbers
  • Output the largest of the three numbers

Output:
Type a Number: 1
Type a Number: 3
Type a Number: 2

Number 3 is the largest


Type a Number: 4
Type a Number: 5
Type a Number: 6

Number 6 is the largest

Type a Number: 9
Type a Number: 7
Type a Number: 8

Number 9 is the largest


Electric Bill

Difficulty: Medium

Title: Electric Bill

Description:
Write a program to calculate and print the electricity bill of a given customer. Prompt the user for their customer ID, name, and unit consumed by the user. Output the total amount to be paid to the customer.

The charge are as follow :

Unit Charge / Unit
up to 199 @1.30
200 - 399 @1.60
400 - 599 @1.90
600 and above @2.10

NOTE: If bill exceeds 400 units then a surcharge of 15% will be charged based upon the total bill and the minimum bill should be of 100 units.

Output:
Customer ID: 1111
Customer Name: Boothe
Units Consumed: 800

Amount Charged @ 2.10 per unit: 1,680
Surcharge Amount: 252
Total Due: 1,932


Customer ID: 1112
Customer Name: Anderson
Units Consumed: 350

Amount Charged @ 1.60 per unit: 560
Surcharge Amount: 0
Total Due: 560


Course Eligibility

Difficulty: Medium

Title: Course Eligibility

Description:
Write a program to determine eligibility for admission to an upper level programming course based on the following criteria:

Eligibility Criteria :

  • Marks in Algebra 1 >= 65 and Marks in Programming Fundamentals >= 70 and Marks in AP Computer Science Principles >= 70 and Total in all three subjects >= 210
OR
  • Total in Algebra I, Geometry, Algebra II and Marks in Programming Fundamentals >= 140
Prompt the user for the following grades:

NOTE: If they have not taken a course then enter a 0
  • Algebra I
  • Geometry
  • Algebra II
  • Programming Fundamentals
  • AP Computer Science Principles
Output the following:
  • Candidate is eligible
  • Candidate is not eligible

Output:
Algebra I: 70
Geometry: 0
Algebra II: 0
Programming Fundamentals: 70
AP Computer Science Principles: 70

Candidate is eligible


Algebra I: 65
Geometry: 0
Algebra II: 0
Programming Fundamentals: 70
AP Computer Science Principles: 70

Candidate is not eligible
Algebra I: 65
Geometry: 0
Algebra II: 0
Programming Fundamentals: 70
AP Computer Science Principles: 0

Candidate is not eligible
Algebra I: 70
Geometry: 0
Algebra II: 0
Programming Fundamentals: 80
AP Computer Science Principles: 0

Candidate is eligible


Days in a month

Difficulty: Medium

Title: Days in a month

Description:
Write a program to find the number of days in a month (including leap years).

  • Prompt the user for the month.
  • If the month is February then prompt the user for the year.
  • Handle the following leap years: 2020, 2024, 2028, 2032
Thirty days hath September, April, June, and November; All the rest have thirty-one, Excepting February alone, And that has twenty-eight days clear.

Output:
Enter a month: September

This is: 30 days


Enter a month: July

This is: 31 days
Enter a month: February
Enter a year: 2024

This is: 29 days
Enter a month: February
Enter a year: 2025

This is: 28 days


Your Height

Difficulty: Medium

Title: Your Height

Description:
Prompt the user for their height in feet and inches, then provide them a menu option to calculate their height in either meters or centimeters. Output their height based upon the menu option chosen.

  • 12 inches in a foot
  • 1 inch equals approximately 0.0254 meters
  • 1 inch equals approximately 2.54 centimeters

Additional Programming notes:
  • Round your output to 2 decimal places
  • Handle if the user does not enter a number in any of the inputs
  • Handle if the user enters a menu option that does not exist

Output:
Height in Feet: 5
Height in Inches: 7
Choose to calculate your height in:
1 - Meters
2 - Centimeters
Choice: 1

Your height in meters is: 1.70


Height in Feet: 5
Height in Inches: 7
Choose to calculate your height in:
1 - Meters
2 - Centimeters
Choice: 2

Your height in centimeters is: 170.18
Height in Feet: 5
Height in Inches: 7
Choose to calculate your height in:
1 - Meters
2 - Centimeters
Choice: a

Not a number
Height in Feet: 5
Height in Inches: 7
Choose to calculate your height in:
1 - Meters
2 - Centimeters
Choice: 3

Not a menu choice
Height in Feet: a

Not a number


Form Triangle

Difficulty: Medium

Title: Form Triangle

Description:
Write a program to check whether a triangle can be formed based upon three values entered by a user for the angles.

To check if a triangle can be formed with given side lengths, use the "Triangle Inequality Theorem": add any two side lengths together, and if the sum is greater than the third side length, then a triangle can be formed.

NOTE: You have to add up all combination of sides to determine if a triangle can be formed.

  • Prompt the user for three values.
  • If the triangle can be formed output: "A triangle can be formed"
  • If the triangle cannot be formed output: "A triangle cannot be formed"

Output:
Side 1: 7
Side 2: 10
Side 3: 5

A triangle can be formed


Side 1: 2
Side 2: 9
Side 3: 13

A triangle cannot be formed
Side 1: 2
Side 2: 13
Side 3: 9

A triangle cannot be formed


Clothing Discount

Difficulty: High

Title: Clothing Discount

Description:
Write a program to determine if the total amount of a sale should be discounted. If the amount is greater than $100, then the amount is discounted by 5%. If the sale is greater than $150 then the amount of the discount is 10%.

At the store shoppers are only allowed to buy 5 things at a time and the following items are sold:

  • Short Sleeve Shirts $10
  • Long Sleeve Shirts: $15
  • Jeans: $30
  • Cargo Pants: $30
  • Jackets: $50
  • Dresses: $35

Output:
Welcome to XYZ store enter the number of the item you would like to purchase:
1 - Short Sleeve Shirts $10
2 - Long Sleeve Shirts: $15
3 - Jeans: $30
4 - Cargo Pants: $30
5 - Jackets: $50
6 - Dresses: $35

Clothing Purchase #1: 4
Clothing Purchase #2: 4
Clothing Purchase #3: 5
Clothing Purchase #4: 1
Clothing Purchase #5: 1

Total Purchase Amount (Before Discount): $130
Discount: 5%
Discount Amount: $6.50
Total Due: $123.50


Date of the Bunny

Difficulty: High

Title: Date of the Bunny

Description:
Implement the calculator for the date of the bunny.

The following algorithm computes the date of Bunny Sunday for any year between 1900 to 2099.

Ask the user to enter a year. Compute the following:

  • a = year % 19
  • b = year % 4
  • c = year % 7
  • d = (19 * a + 24) % 30
  • e = (2 * b + 4 * c + 6 * d + 5) % 7
  • dateofbunny = 22 + d + e
More pieces to implement:
  • If you subtract dateofbunny - 31 you will get the exact date for that year if the dayofbunny is greater than 31
  • The exception is for four special years (1954, 1981, 2049, or 2076) then subtract 7 from the date as well.
  • Output the correct ending for the number (ex 19th):
    • If the number ends in 1, the suffix is –st.
    • If the number ends in 2, the suffix is –nd.
    • If the number ends in 3, the suffix is –rd.
    • If the number ends in 4-9, the suffix is –th.
    • If the number is a multiple of 10, the suffix is –th.
    • The numbers 11-19 also use the suffix –th.
Your program should print an error message if:
  • The user provides a date that is out of range then print: "Year out of range"
  • If the user does not enter a date (i.e. a String) then print: "Not a number"

Output:
Year: 1981

April 19th


Year: 1899

Year out of range
Year: fred

Not a number
Year: 2025

April 20th



Computer Science