Write a Python program and java program that asks the user for a number in the range of 1 through 7. The program should display the corresponding day of the week
Write a Python program that asks the user for a number in the range of 1 through 7. The program should display the corresponding day of the week, where 1=Monday, 2=Tuesday, 3=Wednesday, 4=Thursday, 5=Friday, 6=Saturday, and 7=Sunday. The program should display an error message if the user enters a number that is outside the range of 1 through 7.
public class DaysOfweek {
public static void main (String args[]) {
/**creates an array of Strings with identifier days and
initialized. This array contains 7 elements */
String days [ ] = {"Monday", "Tuesday", "Wednesday", "Thursday",
"Friday", "Saturday", "Sunday"};
for(int i=0;i<7;i++){
System.out.println(days[i]);
}
}
}
one more example:
To get Sunday as 1 through Saturday as 7, this is the simplest solution to your question:
datetime.date.today().toordinal()%7 + 1
All of them:
import datetime
today = datetime.date.today()
sunday = today - datetime.timedelta(today.weekday()+1)
for i in range(7):
tmp_date = sunday + datetime.timedelta(i)
print tmp_date.toordinal()%7 + 1, '==', tmp_date.strftime('%A')
one more example:
To get Sunday as 1 through Saturday as 7, this is the simplest solution to your question:
All of them:
Output:
need an explanation for this answer? contact us directly to get an explanation for this answer