1. How do I keep prompting the user for more dates until the user has indicated that he/she wishes to quit.
2. Computation of the day of the week MUST be moved to its own function.
3. How do I verify that the date entered is valid. For example, April only has 30 days so the user will not be allowed to enter April 31 as a valid date. See input section below to see how this is to be accomplished.
4. I have 2 functions which will help me ensure that the user enters a valid date. How do I integrate these functions into your solution.
# dictionary of day names for output.
dayNames = {0:"Saturday", 1:"Sunday", 2:"Monday", 3:"Tuesday", 4:"Wednesday",
5:"Thursday", 6:"Friday"}
# Get user input: month, day, year
sys.stdout.write("Please enter the day of the month (1-31): ")
dayOfMonth = int(sys.stdin.readline().strip())
sys.stdout.write("Please enter the month (1-12): ")
month = int(sys.stdin.readline().strip())
sys.stdout.write("Please enter the year: ")
year = int(sys.stdin.readline().strip())
# Make adjustments for forumla
if month == 1 or month == 2:
month = month + 12
year = year - 1
# Compute coefficients
century = year/100
yearInCentury = year % 100
# Compute the day using Zeller's congruence
dayOfWeek = (dayOfMonth + ((month + 1)* 26)/10 + yearInCentury +
(yearInCentury/4) + (century/4) - 2*century) % 7
# Output answer
print "Day of week is %s" % dayNames[dayOfWeek]
12 7522
Please post back using [ code ] [ /code ] (but without spaces) tags around your code. Otherwise, we can't see the structure of you code. Thanks.
for example
here is the code...
# dictionary of day names for output.
dayNames = {0:"Saturday", 1:"Sunday", 2:"Monday", 3:"Tuesday", 4:"Wednesday",
5:"Thursday", 6:"Friday"}
# Get user input: month, day, year
sys.stdout.write("Please enter the day of the month (1-31): ")
dayOfMonth = int(sys.stdin.readline().strip())
sys.stdout.write("Please enter the month (1-12): ")
month = int(sys.stdin.readline().strip())
sys.stdout.write("Please enter the year: ")
year = int(sys.stdin.readline().strip())
# Make adjustments for forumla
if month == 1 or month == 2:
month = month + 12
year = year - 1
# Compute coefficients
century = year/100
yearInCentury = year % 100
# Compute the day using Zeller's congruence
dayOfWeek = (dayOfMonth + ((month + 1)* 26)/10 + yearInCentury +
(yearInCentury/4) + (century/4) - 2*century) % 7
# Output answer
print "Day of week is %s" % dayNames[dayOfWeek]
I'll work on the date thing. I want you to do the loop based on your other post, OK?
Here's some date stuff for you to play with. Also look at the datetime module in the standard library. -
import calendar
-
year = 2006
-
month = 10
-
day = 31
-
calendar.setfirstweekday(calendar.SUNDAY)
-
fd, nDays = calendar.monthrange(year, month)
-
if day > nDays:
-
print "not a valid date"
-
Please use [code] tags when you post back. Thanks,
Barton
- Input: the user inputs the amount in one of the following forms:
-
# C value
-
# F value
-
# CM value
-
# I value
-
#
-
# C - is a celcius value. Program converts to fahrenheit
-
# F - is a fahrenheit value. Program converts to celcius
-
# CM - is a centimetre value. Program converts to inches
-
# I - is an inch value. Program converts to centimetres
-
-
import sys
-
-
# Prompt user
-
sys.stdout.write( "Enter a value to convert: ")
-
-
# Read in a line from the keyboard
-
lineElements = sys.stdin.readline().split(" ")
-
-
# If the value is Celcius, convert to Fahrenheit
-
if lineElements[0].lower() == 'c':
-
print "%s degress celcius is %.2f degrees fahrenheit." \
-
% ( lineElements[1].strip(), (float(lineElements[1])*9.0/5.0)+32)
-
-
# If the value is Fahrenheit, convert to Celcius
-
elif lineElements[0].lower() == 'f':
-
print "%s degress fahrenheit is %.2f degrees celcius." \
-
% ( lineElements[1].strip(), (float(lineElements[1]) - 32) * 5.0/9.0)
-
-
# if the value is in centimetres, convert to inches
-
elif lineElements[0].lower() == 'cm':
-
print "%s centimetres is %.2f inches." % ( lineElements[1].strip(),
-
(float(lineElements[1]) * 0.3937))
-
-
# if the value is in inches, convert to centimetres
-
elif lineElements[0].lower() == 'i':
-
print "%s inches is %.2f centimetres." % ( lineElements[1].strip(),
-
(float(lineElements[1]) * 2.54))
Sorry wrong code...this is the right code - import sys
-
-
# dictionary of day names for output.
-
dayNames = {0:"Saturday", 1:"Sunday", 2:"Monday", 3:"Tuesday", 4:"Wednesday",
-
5:"Thursday", 6:"Friday"}
-
-
# Get user input: month, day, year
-
sys.stdout.write("Please enter the day of the month (1-31): ")
-
dayOfMonth = int(sys.stdin.readline().strip())
-
-
sys.stdout.write("Please enter the month (1-12): ")
-
month = int(sys.stdin.readline().strip())
-
-
sys.stdout.write("Please enter the year: ")
-
year = int(sys.stdin.readline().strip())
-
-
# Make adjustments for forumla
-
if month == 1 or month == 2:
-
month = month + 12
-
year = year - 1
-
-
# Compute coefficients
-
century = year/100
-
yearInCentury = year % 100
-
-
# Compute the day using Zeller's congruence
-
dayOfWeek = (dayOfMonth + ((month + 1)* 26)/10 + yearInCentury +
-
(yearInCentury/4) + (century/4) - 2*century) % 7
-
-
# Output answer
-
print "Day of week is %s" % dayNames[dayOfWeek]
Sorry wrong code...this is the right code - import sys
-
-
# dictionary of day names for output.
-
dayNames = {0:"Saturday", 1:"Sunday", 2:"Monday", 3:"Tuesday", 4:"Wednesday",
-
5:"Thursday", 6:"Friday"}
-
-
# Get user input: month, day, year
-
sys.stdout.write("Please enter the day of the month (1-31): ")
-
dayOfMonth = int(sys.stdin.readline().strip())
-
-
sys.stdout.write("Please enter the month (1-12): ")
-
month = int(sys.stdin.readline().strip())
-
-
sys.stdout.write("Please enter the year: ")
-
year = int(sys.stdin.readline().strip())
-
-
# Make adjustments for forumla
-
if month == 1 or month == 2:
-
month = month + 12
-
year = year - 1
-
-
# Compute coefficients
-
century = year/100
-
yearInCentury = year % 100
-
-
# Compute the day using Zeller's congruence
-
dayOfWeek = (dayOfMonth + ((month + 1)* 26)/10 + yearInCentury +
-
(yearInCentury/4) + (century/4) - 2*century) % 7
-
-
# Output answer
-
print "Day of week is %s" % dayNames[dayOfWeek]
Awesome! You can post using tags! That's great.
Now you have some more things to work on after you copy the pieces from my last posts.
You CAN copy and paste to and from the forum, right?
I am trying to incorporate this code into this program but I am have trouble getting to run properly. These functions are used for leap years... - def isLeap(year):
-
if year % 400 == 0:
-
return True
-
elif year % 4 == 0 and year % 100 !=0:
-
return True
-
else:
-
return False
-
-
def computeMaxDay(month, year):
-
maxDays = {1:31, 2:28, 3:31, 4:30, 5:31, 6:30, 7:31, 8:31, 9:30, 10:31, 11:30, 12:31}
-
if month == 2:
-
if isLeap(year):
-
return 29
-
return maxDays[month]
I am trying to incorporate this code into this program but I am have trouble getting to run properly. These functions are used for leap years... - def isLeap(year):
-
if year % 400 == 0:
-
return True
-
elif year % 4 == 0 and year % 100 !=0:
-
return True
-
else:
-
return False
-
-
def computeMaxDay(month, year):
-
maxDays = {1:31, 2:28, 3:31, 4:30, 5:31, 6:30, 7:31, 8:31, 9:30, 10:31, 11:30, 12:31}
-
if month == 2:
-
if isLeap(year):
-
return 29
-
return maxDays[month]
since
>>> 1996 %4
0
>>> 1997 %4
1
>>> 2000 %4
0
>>> not(2008 % 4)
True
>>>
You can simply -
def IsLeapYear(year):
-
return not (year % 4)
-
I am to follow that exact code and not change it...this is what I have got but I am having trouble getting it to work. - import sys
-
-
# dictionary of day names for output.
-
dayNames = {0:"Saturday", 1:"Sunday", 2:"Monday", 3:"Tuesday", 4:"Wednesday",
-
5:"Thursday", 6:"Friday"}
-
-
# Get user input: month, day, year
-
sys.stdout.write("Please enter the day of the month (1-31): ")
-
dayOfMonth = int(sys.stdin.readline().strip())
-
-
sys.stdout.write("Please enter the month (1-12): ")
-
month = int(sys.stdin.readline().strip())
-
-
sys.stdout.write("Please enter the year: ")
-
year = int(sys.stdin.readline().strip())
-
# Determining Leap year.
-
def isLeap(year):
-
if year % 400 == 0:
-
return True
-
elif year % 4 == 0 and year % 100 !=0:
-
return True
-
else:
-
return False
-
# Determining Maximum number of days in each month possible.
-
def computeMaxDay(month, year):
-
maxDays = {1:31, 2:28, 3:31, 4:30, 5:31, 6:30, 7:31, 8:31, 9:30, 10:31, 11:30, 12:31}
-
if month == 2:
-
if isLeap(year):
-
return 29
-
return maxDays[month]
-
-
# Make adjustments for forumla
-
if month == 1 or month == 2:
-
month = month + 12
-
year = year - 1
-
-
# Compute coefficients
-
century = year/100
-
yearInCentury = year % 100
-
-
# Compute the day using Zeller's congruence
-
dayOfWeek = (dayOfMonth + ((month + 1)* 26)/10 + yearInCentury +
-
(yearInCentury/4) + (century/4) - 2*century) % 7
-
-
-
-
# Output answer
-
print "Day of week is %s" % dayNames[dayOfWeek]
-
-
# Output Quit Program
-
while(input != "quit\n"):
-
input = sys.stdin.readline()
-
print "you typed : " + input
since
>>> 1996 %4
0
>>> 1997 %4
1
>>> 2000 %4
0
>>> not(2008 % 4)
True
>>>
You can simply -
def IsLeapYear(year):
-
return not (year % 4)
-
This ought to help. Let us know how this ends up, please. -
-
import sys
-
-
# dictionary of day names for output.
-
dayNames = {0:"Saturday", 1:"Sunday", 2:"Monday", 3:"Tuesday", 4:"Wednesday",
-
5:"Thursday", 6:"Friday"}
-
# Determining Leap year.
-
def isLeap(year):
-
if year % 400 == 0:
-
return True
-
elif year % 4 == 0 and year % 100 !=0:
-
return True
-
else:
-
return False
-
# Determining Maximum number of days in each month possible.
-
def computeMaxDay(month, year):
-
maxDays = {1:31, 2:28, 3:31, 4:30, 5:31, 6:30, 7:31, 8:31, 9:30, 10:31, 11:30, 12:31}
-
if month == 2 and isLeap(year):
-
return 29
-
return maxDays[month]
-
def Zellers(day, month, year):
-
# Make adjustments for forumla
-
if month == 1 or month == 2:
-
month = month + 12
-
year = year - 1
-
-
# Compute coefficients
-
century = year/100
-
yearInCentury = year % 100
-
-
# Compute the day using Zeller's congruence
-
dayOfWeek = (day + ((month + 1)* 26)/10 + yearInCentury +
-
(yearInCentury/4) + (century/4) - 2*century) % 7
-
# Output answer
-
return dayNames[dayOfWeek]
-
-
# Your post says that you are not to change the code
-
# But your code needs to change here so that you can prevent errors
-
# Because "quit\n" can't be converted to an int.
-
# but here is a "brute force" way to do it
-
print "Enter 'q' to quit" # anything other than a number works
-
while True:
-
try:
-
# Get user input: month, day, year
-
sys.stdout.write("Please enter the day of the month (1-31): ")
-
dayOfMonth = int(sys.stdin.readline().strip())
-
sys.stdout.write("Please enter the month (1-12): ")
-
month = int(sys.stdin.readline().strip())
-
if month > 12:
-
print "whatever"
-
continue # Bounce back to top of loop
-
-
sys.stdout.write("Please enter the year: ")
-
year = int(sys.stdin.readline().strip())
-
-
if dayOfMonth > computeMaxDay(month, year):
-
print "whatever"
-
continue # Bounce back to top of loop
-
dayName = Zellers(dayOfMonth, month, year)
-
print "Day of week is %s" % dayName
-
print
-
except ValueError:
-
break
-
Keep posting,
Barton
I know this thread is old but.. its was just what I needed. Thanks for being there. I incorporated your bits parts in my script along with a little extra flair. Here have a look. Thanks again. - #! /usr/bin/env python2.5
-
import os,sys
-
-
#print out months of the year
-
-
print "Please type in your birthdate by the year, month and day."
-
-
months = [
-
'Janurary',
-
'February',
-
'March',
-
'April',
-
'May',
-
'June',
-
'July',
-
'August',
-
'September',
-
'October',
-
'November',
-
'December'
-
]
-
-
dayNames = {0:"Saturday", 1:"Sunday", 2:"Monday", 3:"Tuesday", 4:"Wednesday", 5:"Thursday", 6:"Friday"}
-
-
#Ending is just a seriies of additions of the endings. Instead of listing them all IE 'th', 'th'
-
# the mulitplicaitons does it for us and keeps it short for reading purposes
-
# IE. 'th, 'th, 'th" is equal to 3 * ['th'] in the code below you could use the paranethsis
-
# or not as the mulitplication happen 1st in all code then the additions.
-
-
endings = ['st', 'nd', 'rd'] + 17 * ['th'] \
-
+ ['st', 'nd', 'rd'] + 7 * ['th'] \
-
+ ['st']
-
-
year = int(raw_input('Year: '))
-
month = int(input('Month (1-12):'))
-
-
if month <= 12 and month >= 1:
-
pass
-
else:
-
sys.exit(`month` + " is not between 1-12")
-
-
dayOfMonth =int(input('Day (1-31): '))
-
-
#if day <= 31 and day >= 1:
-
# print " "
-
#else:
-
# sys.exit(`day` + " is not between 1-12")
-
-
-
month_number = int(month)
-
day_number = int(dayOfMonth)
-
-
month_name = months[month_number-1]
-
ordinal = `dayOfMonth` + endings[day_number -1]
-
-
-
if month == 1 or month == 2:
-
month = month + 12
-
year = year - 1
-
-
century = year/100
-
yearInCentury = year % 100
-
-
-
print dayOfMonth
-
dayOfWeek = (dayOfMonth + ((month +1) * 26) /10 + yearInCentury + (yearInCentury/4) + (century/4) - 2*century) % 7
-
-
-
print month_name + " " + ordinal + "," + "%s is a %s" % (year, dayNames[dayOfWeek])
-
-
-
Sign in to post your reply or Sign up for a free account.
Similar topics
by: aarklon |
last post by:
Hi folks,
I found an algorithm for calculating the day of the week here:-
http://www.faqs.org/faqs/calendars/faq/part1/index.html
in the section titled 2.5 what day of the week was 2 august...
|
by: bratiskovci |
last post by:
Hello,
I am trying to write a few programs using python but don't really know where to start...I am completely CONFUSED.
My first program deals with conversion between fahrenheit and celcius...
|
by: bratiskovci |
last post by:
I have to write a genetics program:
This is the INPUT:
25 sets of genetic sequences are provided. Each sample is provided in a specific format so that my program may read in the genetic...
|
by: iHateProg |
last post by:
I am looking for a day of birth generator that will use MONTH DATE AND YEAR born to generate the BIRTH DAY from 1900-2000.
Ex:- If someone was born on April 10, 1990. what day of the week were they...
|
by: ntraviglia |
last post by:
Can anyone help?
I have to write a zellers congruence program with visual. Anyone have any sample code or maybe the actual zeller program in visual they can give me, I am totally lost.
|
by: Justn226 |
last post by:
anyone have any idea why i am not getting any return values? it will return the words just not the numbers?
HTML
<HTML>
<head>
<title>Zellers Carpeting Cost Estimate</title>
</head>
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |