Using tuples and input functions
Question:
My task (for class): Write a program that takes a month number (like 3) and outputs the month name (like March). Use a list or tuple to store all of the month names and then use the month number to index.
Example:
>>> Enter a month number from 1 to 12: 3
>>> That is March.
This is what I’ve tried:
from collections import namedtuple
month = int(input('Any month number between 1-4: '))
month_name = namedtuple('Month',['January','February','March','April',])
month_number = month_name(1,2,3,4)
Answers:
You can use this code for the purpose:
month_number = (int(input("Enter month Number : ")))-1
month_name = ('January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December'
)
for i in range(len(month_name)):
if month_number==i:
print(month_name[i])
Try this:
monthes=['January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December']
while True: #repeat asking for the number
index=input('Enter a month number from 1 to 12: ')
print(montheis[int(index)-1])
i used ‘monthes[int(index)-1]’ be cause in the indext of al list or eny itmes type 0
means the first item and 1
the second one, and so on, and -1
the last item and -2-
the item be fore last
My task (for class): Write a program that takes a month number (like 3) and outputs the month name (like March). Use a list or tuple to store all of the month names and then use the month number to index.
Example:
>>> Enter a month number from 1 to 12: 3
>>> That is March.
This is what I’ve tried:
from collections import namedtuple
month = int(input('Any month number between 1-4: '))
month_name = namedtuple('Month',['January','February','March','April',])
month_number = month_name(1,2,3,4)
You can use this code for the purpose:
month_number = (int(input("Enter month Number : ")))-1
month_name = ('January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December'
)
for i in range(len(month_name)):
if month_number==i:
print(month_name[i])
Try this:
monthes=['January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December']
while True: #repeat asking for the number
index=input('Enter a month number from 1 to 12: ')
print(montheis[int(index)-1])
i used ‘monthes[int(index)-1]’ be cause in the indext of al list or eny itmes type 0
means the first item and 1
the second one, and so on, and -1
the last item and -2-
the item be fore last