【问题标题】:pseudocode - zellers congruence - python伪代码 - zellers congruence - python
【发布时间】:2014-07-29 09:00:29
【问题描述】:

我正在上我的第一堂编程课(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

【问题讨论】:

  • 看起来不错。你的问题到底是什么?
  • 如果用户输入了有效的数据,那么如果你正确地转录了 Zeller 的一致性,它或多或少都会起作用——我没有检查过。你的输出应该反映输入和答案,这样你就可以知道计算机认为你输入了什么。您可能需要考虑用户输入月份数字,例如 0、-1、13 等;您可能应该考虑将它们输入诸如 -1、0、32 之类的日期(当月份为 11、4、6 或 9 时为 31;当月份为 2 时为 29 或 30)。也许你也应该限制年份的范围。
  • 非常感谢你们!我也很担心......在伪代码中限制输入范围的好方法是什么?我是一个完整的初学者 - 我也担心我修改 mm 和 yyyy 输入的部分。甚至可以修改它们并将它们存储为python中的m和y变量吗?

标签: python date pseudocode


【解决方案1】:

我最近开始学习 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-11-29
    • 2013-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-15
    • 1970-01-01
    相关资源
    最近更新 更多