【发布时间】: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