/CPT-Questions

Exercises for beginners

Primary LanguagePython

CPT-Questions

Exercises for beginners

cent_to_inches.py --

  1. Write a program that asks the user to enter a length in centimeters. If the user enters a negative length, the program should tell the user that the entry is invalid. Otherwise, the program should convert the length to inches and print out the result. There are 2.54 centimeters in an inch.

temperature_converter.py --
2. Ask the user for a temperature. Then ask them what units, Celsius or Fahrenheit, the temperature is in. Your program should convert the temperature to the other unit. The conversions are F = 9 5C + 32 and C = 5 9 (F 􀀀 32).

Temperature_classifier.py --
3. Ask the user to enter a temperature in Celsius. The program should print a message based on the temperature: • If the temperature is less than -273.15, print that the temperature is invalid because it is below absolute zero. • If it is exactly -273.15, print that the temperature is absolute 0. • If the temperature is between -273.15 and 0, print that the temperature is below freezing. • If it is 0, print that the temperature is at the freezing point. • If it is between 0 and 100, print that the temperature is in the normal range. • If it is 100, print that the temperature is at the boiling point. • If it is above 100, print that the temperature is above the boiling point.

student_classifier.py --
4. Write a program that asks the user how many credits they have taken. If they have taken 23 or less, print that the student is a freshman. If they have taken between 24 and 53, print that they are a sophomore. The range for juniors is 54 to 83, and for seniors it is 84 and over.

guess_the number.py --
5. Generate a random number between 1 and 10. Ask the user to guess the number and print a message based on whether they get it right or not.

cost_calculator.py --
6. A store charges $12 per item if you buy less than 10 items. If you buy between 10 and 99 items, the cost is $10 per item. If you buy 100 or more items, the cost is $7 per item. Write a program that asks the user how many items they are buying and prints the total cost.

close_numbers.py --
7. Write a program that asks the user for two numbers and prints Close if the numbers are within .001 of each other and Not close otherwise.

leap_year_identifier.py --
8. A year is a leap year if it is divisible by 4, except that years divisible by 100 are not leap years unless they are also divisible by 400. Write a program that asks the user for a year and prints out whether it is a leap year or not.

divisors.py --
9. Write a program that asks the user to enter a number and prints out all the divisors of that number. [Hint: the % operator is used to tell if a number is divisible by something. See Section 3.2.]