我正在编程(python)中学习第一堂课。我的任务是编写伪代码,然后编写一个python程序,它将使用dd / mm / yyyy作为输入来计算星期几。我正在使用Zeller's congruence来计算星期几。这是我作为伪代码的第一次尝试 - 我希望得到一些反馈。我不确定我是否走在正确的轨道上。我的逻辑清楚吗?初学者能将它翻译成python吗?任何提示或帮助将不胜感激。
Program Calculate the Day
Purpose of the program: this program inputs a date and calculates the day of the week on which it occurred.
Date: 6/4/14
# Prompt a user to type his/her birthdate as three different values
print "In what month were you born?"
Input mm
print "on what day of the month?"
Input dd
print "In what year were you born?"
Input yyyy
# adjusts inputs to account for algorithm if birthdate occurs on Jan or Feb
if input mm = 1 or mm = 2:
mm = mm + 12
and
yyyy = yyyy - 1
# stores inputs as variables for use in zellers congruence
store mm as m
store dd as q
store yyyy as y
# Calculate the day of the week and store it in a variable named dow
dow = ( ( q + ( (m + 1) * 26 // 10 )+ Y +( Y // 4 )+ 6 * (Y // 100 )+ (Y // 400 )) % 7 )
Output "you were born on a Day" dow
# Notice that there is no space between the word “Day” and the number; this helps it look like one of those “number for days” cultures
答案 0 :(得分:0)
我最近开始学习python,并为您的问题找到了解决方案。如果您喜欢或有任何疑问,请告诉我
#importing functions
import time
import sys
import datetime
import math
start=time.perf_counter()#starting timer
hi=str(input('enter date in mm/dd/yyyy'))#asking for input
try:
date= datetime.datetime.strptime(hi,'%m/%d/%Y').date()#checking for format
d2=str(date.year)
l3=d2[:2] #breaking up string
l4=d2[2:]
C=int(l3)
D=int(l4)
K=date.day
M=date.month
if M==1:# making zellers month system
D=D-1
M=11
elif M==2:
D=D-1
M=12
else:
M=M-2
#breaking up the equation.The equation is f = k + [(13*m-1)/5] + D + [D/4] +
[C/4] - 2*C
f=13*M
g=f-1
a=g/5
a=math.floor(a) #cutting off the decimals
z=a+K
b=D/4
b=math.floor(b)
c=C/4
c=math.floor(c)
d=2*C
f1=(z+b+c+D)
d1=f1-d
l=d1 % 7 # getting remainder
l=math.floor(l)#cutting off decimals
if (l==1):#using remainder to find day of week
print('The day is Mon')
elif (l==2):
print('The day is Tue')
elif (l==3):
print('The day is Wed')
elif (l==4):
print('The day is Thu')
elif (l==5):
print('The day is Fri')
elif (l==6):
print('The day is Sat')
elif (l==7 or 0):
print('The day is Sun')
#printing the day
except Exception:
print('Please enter date in correct format')#error statement
else:
#calculating final time
end=time.perf_counter()
a5=end-start
message='The time taken is %s seconds'
print(message % a5)#displaying time