伪代码 - 泽勒斯合同 - python

1 投票
1 回答
3102 浏览
提问于 2025-04-18 09:03

我正在上我的第一门编程课(学习Python)。我的作业是写伪代码,然后再写一个Python程序,用来计算输入的日期(格式是日/月/年)对应的星期几。我使用的是泽勒公式来计算星期几。这是我第一次尝试写伪代码——我希望能得到一些反馈。我不确定自己是否在正确的方向上。我的逻辑清晰吗?一个初学者能否把这个转成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

1 个回答

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

撰写回答