In: Computer Science
Chapter 8 Programming exercise 6 "Days of each month"
Original Exercise:
Design a program that displays the number of days in each month.
The program’s output should be similar to this:
January has 31 days.
February has 28 days.
March has 31 days.
April has 30 days.
May has 31 days.
June has 30 days.
July has 31 days.
August has 31 days.
September has 30 days.
October has 31 days.
November has 30 days.
December has 31 days.
The program should have two parallel arrays: a 12-element String
array that is initialized with the names of the months, and a
12-element Integer array that is initialized with the number of
days in each month. To produce the output specified, use a loop to
step through the arrays getting the name of a month and the number
of days in that month.
Modifications:
Use a single, two-dimensional array that has strings
throughout.
Example array:
month_array = [ ["jan", "31"],
["feb", "28"] ]
Using multiple lines to separate your data will make it easier to write the code without mistakes.
Use a while loop to iterate through the array and print the same output as shown above.
Turn IN:
No Raptor flowchart is required.
Thonny Python file (or Python file is written in any
IDE/program)
Turn in the actual
Python code file "file_name.py"
Original code
#month array
month_array = ["January", "February", "March", "April",
"May", "June", "July", "August", "September",
"October", "November", "December"]
#days array with integer values specified number of days
days_array = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
#simultaneously iterate both array ahnd prints an output
for (a, b) in zip(month_array, days_array):
print (a, "has", b, "days.")
Please refer below screenshot of code to understand indentation of code and output.
Modification :
#month array
month_array = [["January", "31"],
["February", "28"],
["March", "31"],
["April", "30"],
["May", "31"],
["June", "30"],
["July", "31"],
["August", "31"],
["September", "30"],
["October", "31"],
["November", "30"],
["December", "31"]]
#to itearte 2 while loops to print desired output
a = 0
while a < (len(month_array)):
b = 0
while b < (len(month_array[a])-1):
print (month_array[a][0], "has", month_array[a][1], "days.")
b = b + 1
a = a + 1
Please refer below screenshot of code to understand indentation of code and output.
Turn IN:
Save your above code in file_name.py(as per your wish whatever file name)