Concept explainers
Calculate whether a date is valid
Program plan:
- Define the function “isLeapYear()”,
- Check whether the year is not divisible by "4",
- If it is true, return “False”.
- Otherwise, check whether the year is divisible by "100",
- If it is true, check whether the year is divisible by "400",
- If it is true, return “True”.
- Otherwise, return “False”.
- Otherwise, return “True”.
- If it is true, check whether the year is divisible by "400",
- Check whether the year is not divisible by "4",
- Define the function “main()”,
- Get the date from the user.
- Assign month, day, and year by splitting the date by "/" using “split()” method
- Typecast month, day, and year to “int” type.
- Check whether either a month is greater than "12" or day is greater than "31",
- If it is true, prints date as invalid.
- Otherwise, Check whether day is less than or equal to "28",
- If the condition is “True”, print as valid.
- Otherwise, check whether month is "2" and day is "29",
- If it is “True” Check whether the return value from “isLeapYear()” is “False”,
- If “True” print invalid.
- Otherwise, print valid.
- Otherwise, check whether day is equal to “31”,
- If “True”, Check whether month is either "2" or "4" or "6" or "11",
- If “True” print invalid.
- Otherwise, print valid.
- Otherwise, print valid.
- If “True”, Check whether month is either "2" or "4" or "6" or "11",
- Call the function “main()”.
This Python program accepts a date in the form month / day / year and prints whether the date is valid or not.
Explanation of Solution
Program:
File name: “Leap.py”
#Define the function isLeapYear()
def isLeapYear(y):
#Check whether the year is not divisible by "4"
if (y % 4) != 0:
#Return false
return False
#Otherwise
else:
#Check whether the year is divisible by "100"
if (y % 100) == 0:
#Check whether the year is divisible by "400"
if (y % 400) ==0:
#Return true
return True
#Otherwise
else:
#Return false
return False
#Otherwise
else:
#Return true
return True
#Define the function main()
def main():
#Get the date from the user
date=eval(input("Enter date"))
'''Assign month, day, and year by splitting the date by "/" using split() method'''
month_Str, day_Str, year_Str = date.split("/")
#Typecast month to int type
mon = int(month_Str)
#Typecast day to int type
d = int(day_Str)
#Typecast year to int type
yr = int(year_Str)
'''Check whether either a month is greater than "12" or day is greater than "31"'''
if mon > 12 or d > 31:
#Print a message
print("This date is invalid.")
#Otherwise
else:
#Check whether day is less than or equal to "28"
if d <= 28:
#Print a message
print("This date is valid.")
#check whether month is "2" and day is "29"
elif mon == 2 and d == 29:
#Check whether the return value is false
if isLeapYear(yr) == False:
#Print a message
print("This date is invalid.")
#Otherwise
else:
#Print a message
print("This date is valid.")
#Check whether day is "31"
elif d == 31:
'''Check whether month is either "2" or "4" or "6" or "11"'''
if mon == 2 or 4 or 6 or 11:
#Print date is invalid
print("This date is invalid")
#Otherwise
else:
#Print date is valid
print("This date is valid")
#Otherwise
else:
#Print dte is valid
print("The date is valid.")
#Call the function main()
main()
Output:
Enter date'6/29/1200'
The date is valid.
>>>
Additional Output:
Enter date'12/32/1500'
This date is invalid.
>>>
Additional Output:
Enter date'13/28/1999'
This date is invalid.
>>>
Want to see more full solutions like this?
Chapter 7 Solutions
Python Programming: An Introduction to Computer Science, 3rd Ed.
- A parking garage charges a $20.00 minimum fee to park for up to three hours. The garage charges an additional $5.00 per hour for each hour or part thereof in excess of three hours. The maximum charge for any given 24- hour period is $50.00. Assume that no car parks for longer than 24 hours at a time. Write a program that calculates and printsthe parking charges for each of three customers who parked their cars in this garage yesterday. You should enter the hours parked for each customer. Your program should print the results in a neat tabular format and should calculate and print the total of yesterday’s receipts. The program should use the function calculateCharges to determine the charge for each customer. Your outputs should appear in the following format: Car Hours Charge1 1.5 20.002 4.0…arrow_forwardWrite a program that accepts a year and determines whether the year is a leap year. Use the mod function. The output should be the variable extra_day, which should be 1 if the year is a leap year and 0 otherwise. The rules for determining leap years in the Gregorian calendar are as follows: All years evenly divisible by 400 are leap years. Years evenly divisible by 100 but not by 400 are not leap years. Years divisible by 4 but not by 100 are leap years. All other years are not leap years. For example, the years 1800, 1900, 2100, 2300, and 2500 are not leap years, but 2400 is a leap year. Solve using Matlabarrow_forwardWrite a program that accepts a year written as a four-digit Arabic (ordinary) numeral and outputs theyear written in Roman numerals. Important Roman numerals are V for 5, X for 10, L for 50, C for 100, Dfor 500, and M for 1,000. Recall that some numbers are formed by using a kind of subtraction of oneRoman “digit”; for example, IV is 4 produced as V minus I, XL is 40, CM is 900, and so on. A few sampleyears: MCM is 1900, MCML is 1950, MCMLX is 1960, MCMXL is 1940, MCMLXXXIX is 1989. Assume theyear is between 1000 and 3000. Your program should include a loop that lets the user repeat thiscalculation until the user says she or he is donearrow_forward
- Write a program that takes a positive integer as the height of the triangle and prints an alphabetical triangle, using lowercase English letters as shown below. Each subsequent row of the triangle should increase in length by two letters. Make sure your program validates the user input. If the user does not input a positive integer, print "Invalid input."arrow_forwardWrite a program that asks users when their birthday is. Use information provided to give them their astrological sign. Each of the twelve signs should display a different horoscope. Use the following dates for each sign, keeping in mind that both month and day must be evaluated for an accurate result. Aries: March 21–April 20 Taurus: April 21–May 21 Gemini: May 22–June 21 Cancer: June 22–July 22 Leo: July 23–August 22 Virgo: August 23–September 23 Libra: September 24–October 23 Scorpio: October 24–November 22 Sagittarius: November 23–December 21 Capricorn: December 22–January 20 Aquarius: January 21–February 19 Pisces: February 20–March 20arrow_forwardWrite a program that takes a bank account balance and an interest rate as an input. And then outputs the value of the account in 5 years. The output should show the value of the account for two different methods of compounding interest: annually and monthly. When compounded annually, the interest is added once per year at the end of the year. When compounded monthly the interest is added in 12 times per year. On annual interest, you can assume that the interest is posted exactly one year from the date of deposit. In other words, you do not have to worry about interest being posted on a specific day of the year, like December 31. Similarly, you can assume monthly interest is posted exactly one month after. Since the account earns interest on the interest, the account should have a higher balance when interest is posted more frequently. Be sure to adjust the interest rate for the time period of the interest. If the rate is 5%, then when posting monthly interest, you use (5/12%). Do your…arrow_forward
- Write a program that takes a bank account balance and an interest rate as an input. And then outputs the value of the account in 5 years. The output should show the value of the account for two different methods of compounding interest: annually and monthly. When compounded annually, the interest is added once per year at the end of the year. When compounded monthly the interest is added in 12 times per year. On annual interest, you can assume that the interest is posted exactly one year from the date of deposit. In other words, you do not have to worry about interest being posted on a specific day of the year, like December 31. Similarly, you can assume monthly interest is posted exactly one month after. Since the account earns interest on the interest, the account should have a higher balance when interest is posted more frequently. Be sure to adjust the interest rate for the time period of the interest. If the rate is 5%, then when posting monthly interest, you use (5/12%). Do your…arrow_forwardWrite a program that reads the full name of a student; and then prints it in the form: last first middle initial. Example: Input: Ali Hamad Al-Harbi Output: Al-Harbi, Ali H. Write a program that reads a string composed of three words separated by a space, and then print each word on a separate line. Input: one two three Output: One two threearrow_forwardWrite a program that begins by reading a number of cents from the users as an integer (this is input from the user). Your program should then compute and display the denominations of the coins that should be used to give that amount of change to a shopper in coin denominations (Assume that the change machine is loaded with pennies, nickels, dimes, quarters, loonies and toonies). The change should be given using as few coins as possible. For example : you input 455 cents, it tells you that you have: • 2 Toonies • 2 quarters • 1 nickle REMEMBER : you need to work with remainders and floor divisionarrow_forward
- Write a program that begins by reading a number of cents from the users as an integer (this is input from the user). Your program should then compute and display the denominations of the coins that should be used to give that amount of change to a shopper in coin denominations (Assume that the change machine is loaded with pennies, nickels, dimes, quarters, loonies and toonies).The change should be given using as few coins as possible. For example : you input 455 cents, it tells you that you have: 2 Toonies 2 quarters 1 nickle REMEMBER : you need to work with remainders and floor divisionarrow_forwardWrite a program that implements a tip calculator. Your program should ask the user for the bill amount and the percentage of the tip, and then output the dollar amount of the tip and the final bill in three different formats. (see sample below) Example: Enter the bill amount: 27.95 Enter tip percentage: 17 A 17.0% tip on $27.95 equals $4.7515. The total bill is: 32.701499999999996 The total bill is $32.7. The total bill is $32.70.arrow_forwardWrite a program that reads an integer age of kids, finds the maximum of them, and counts its occurances, finds the minimum of them, and counts its occurances. And Find the average ages. Assum that the input ends with number -1.arrow_forward
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning