Question
Python

3. Date Printer Write a program that reads a string from the user containing a date in the form mm/dd/yyyy. It should print t
2 0
Add a comment Improve this question Transcribed image text
✔ Recommended Answer
Answer #1

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

#code

#this program does not use any existing library
#creating a list of all month names

months=['January','February','March','April','May','June','July',
        'August','September','October','November','December']

#reading input from user, splitting by '/' character to get a list of strings
date_input=input('Enter date in the format mm/dd/yyyy: ').split('/')
#assuming user always enter valid input
#extracting month value, decrementing it to convert to list index, assign the
#month at the corresponding index to a month variable

month=months[int(date_input[0])-1]
#converting 2nd input to int to get day value
day=int(date_input[1])
#converting 3rd input to int to get year value
year=int(date_input[2])
#displaying in the format MONTH dd, yyyy
print('The date is {} {}, {}'.format(month,day,year))

#output

Enter date in the format mm/dd/yyyy: 08/11/1993

The date is August 11, 1993

> This code works up to an extent, your assumption applies that if the user input the dates correctly, but in cases they? The months will raise an error, if they go out of range, but if a user put day of 50, the code will accept.

I did the following and i think i tested all possible scenarios, and test results are good:

code#

import datetime


u_date = input("Enter date by mm/dd/yyyy: ").split('/')

month = int(u_date[0])

day = int(u_date[1])

year = int(u_date[2])

x = datetime.datetime(year, month, day)

print(x.strftime("%B"),x.strftime("%d"),',',x.strftime("%Y") )

Heino Plaatjies Thu, Mar 24, 2022 3:05 AM

Add a comment
Know the answer?
Add Answer to:
Python 3. Date Printer Write a program that reads a string from the user containing a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
  • 3. Date Printer Write a program that reads string from the user containing a date in...

    3. Date Printer Write a program that reads string from the user containing a date in the form mm/dd/yyyy. It should print the date in the form March 12, 2014 . ( it is starting out with python third edition ) Can anyone please do it by python program .

  • Write a program that reads a string from the user containing a date in the form...

    Write a program that reads a string from the user containing a date in the form mm/dd/yyyy. It should print the date in the format March 12, 2018   I am asking for help in basic python please

  • IN PYTHON, Write a program that reads a string from the user containing a date in...

    IN PYTHON, Write a program that reads a string from the user containing a date in the form mm/dd/ yyyy. It should print the date in the form April 12, 2017. Hint: Get the "mm" from the user entered date "mm/dd/yyyy", then convert the "mm" into a month number. You may use an list month_list = ['January', 'February','March','April', 'May','June', 'July','August', 'September', 'October', 'November', 'December']. Use the month number you got from "mm" to get the month name.

  • Write a program that accepts a date from the user in the form mm/dd/yyyy

    # C PROGRAMMING LANGUAGEWrite a program that accepts a date from the user in the form mm/dd/yyyy and then dis- plays it in the form month dd, yyyy, where month is the name of the month: Enter a date (mm/dd/yyyy): 2/17/2011 You entered the date February 17, 2011 Store the month names in an array that contains pointers to strings.

  • C Program Question 1: Write a program that reads a date from the keyboard and tests...

    C Program Question 1: Write a program that reads a date from the keyboard and tests whether it contains a valid date. Display the date and a message that indicates whether it is valid. If it is not valid, also display a message explaining why it is not valid. The input date will have the format: mm/dd/yyyy Note that there is no space in the above format. A date in this format must be entered in one line. A valid...

  • Question 1: Write a program in C that reads a date from the keyboard and tests...

    Question 1: Write a program in C that reads a date from the keyboard and tests whether it contains a valid date. Display the date and a message that indicates whether it is valid. If it is not valid, also display a message explaining why it is not valid. The input date will have the format: mm/dd/yyyy Note that there is no space in the above format. A date in this format must be entered in one line. A valid...

  • anyString.substr(x, n) - Returns a copy of a substring. The substring is n characters long and...

    anyString.substr(x, n) - Returns a copy of a substring. The substring is n characters long and begins at position x of anyString. Write a program that reads a string from the user containing a date in the format mm/dd/yyyy. You have to use above substring method to extract the various fields from the format. It should print the date in the form Month Date, Year. Validate:  Exit the program with error message as “Invalid date format” if length of...

  • Write a program that prompts the user to enter the date in mm/dd/yyyy format. The program...

    Write a program that prompts the user to enter the date in mm/dd/yyyy format. The program should use a single input statement to accept the date, storing each piece of the date into an appropriate variable. Demonstrate that the input was obtained correctly by outputting the month, day, and year to the screen. Sample output: Enter (date (mm/dd/yyyy): 02/08/2011 Month entered: 2 Day entered: 8 Year entered: 2011 Challenge: Although the user entered 02, C++ drops the 0 as insignificant...

  • 5. Write a program in, "Python" that reads 2 numbers from the user. The program should...

    5. Write a program in, "Python" that reads 2 numbers from the user. The program should then print out whether the first number is evenly divisible by the second number.

  • in python Write a program that reads the date of birth from the user as 3...

    in python Write a program that reads the date of birth from the user as 3 integer values (day, month, year) and then computes and prints their exact age in days. You need to take into account the leap You may not use any built-in functions related to date and time! Example: Someone born on 01/01/2000 is 365 days * 18 years + 5 days (since the years 2000, 2004, 2008, 2012, and 2016 are all leap years) + 352...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT