用函数编写程序 [英] write a program using functions

查看:69
本文介绍了用函数编写程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好!我正在尝试使用函数编写程序(1个模块是函数,1个模块是主程序并导入函数)来回答这个问题:



zeller的同余是一种计算星期几的算法。公式为:



h =(q +⌊26(m + 1)10⌋+ k +⌊k4⌋+⌊j4⌋+ 5j)%7

其中:



-⌊...⌋表示地板部门

-h是一周中的某一天(0 - 周六, 1 - 周日,2 - 周一,3 - 周二,4 - 周三,5 - - 周日,6 - 周五)

-q是一个月中的某天

- m是月份(3月 - 3月,4月 - 4月,12月 - 12月)。 1月和2月计为上一年的第13和第14个月,例如对于2013年1月25日,您将使用2012年第13个月的第25天。

-j是世纪,例如20 for 2013

-k是本世纪的一年,例如2013年13月。



:使用提示用户输入年份(例如2008年),月份(例如1-12)和日期的功能编写程序月份(例如1-31),然后显示星期几的名称。例如,对于2013年,第1个月和第25个月的某天,星期几是星期五;用户输入2013,1和25以响应您的提示,然后计算h为6,然后输出星期五。运行该程序五个不同的日期。测试必须包括不同的月份,包括1月和2月,不同的世纪和不同的年份。





就我的计划而言,这是什么我到目前为止:



功能模块:

 def day(q,m,j,k ,):
h =(q +( 26 )(m + 1))/(( 10 )+ k +(k / 4)+(j / 4)+(5 * j))%7
如果 h == 1:
day = 星期日
elif h == 2:
day = 星期一
elif h == 3:
day = 星期二
elif h == 4:
day = Wednesday
elif h == 5:
day = 星期四
elif h == 6:
day = Friday
else:
day = Saturday
返回





主要模块:

 import q5_function 

q = int(输入( 输入月中的某一天))
m = int(输入( 输入月份) )
j = int(输入( 输入世纪))
k = int(输入( 输入世纪年))
day = q5_function。日(q,m,k,j)
打印(日)







这个是我在尝试运行程序时得到的错误消息:



输入月份1

输入月份1

输入世纪20

输入世纪10年份/>


Traceback(最近一次调用最后一次):

文件/Users/nathandavis9752/CP104/davi0030_a4/src/q5_main.py,第18行,在< module>

day = q5_function.day(q,m,k,j)

文件/ Users / nathandavis9752 / CP104 / davi0030_a4 / src / q5_function .py,第13行,白天

h =(q +(26)(m + 1))/((10)+ k +(k / 4)+(j / 4)+(5 * j))%7

TypeError:'int'对象不可调用







谢谢大家的帮助!

解决方案

我唯一能看到的是,在我拥有的所有python引用中,它将是



 h ==  1 







(注意空格)



我想知道是否缺少空格(虽然大多数语言都没有重新抛出解析器......





顺便说一句,它的Zeller一致,你放弃了'Z'


refs: http://www.tutorialspoint.com/python/python_if_else .htm [ ^ ]



http://stackoverflow.com/questions/ 19371643 / python-if-elif-else-statement [ ^ ]


Hi there! I'm trying to write a program using functions (1 module being the function, and 1 module being the main program and importing the function) to answer this question:

zeller's congruence is an algorithm to calculate the day of the week. The formula is:

h=(q+⌊26(m+1)10⌋+k+⌊k4⌋+⌊j4⌋+5j)%7
where:

-⌊…⌋ indicates floor division
-h is the day of the week (0 - Saturday, 1 - Sunday, 2 - Monday, 3 - Tuesday, 4 - Wednesday, 5 - -Thursday, 6 - Friday)
-q is the day of the month
-m is the month (3 - March, 4 - April, ... 12 - December). January and February are counted as months 13 and 14 of the previous year, e.g. for January 25 2013, you would use the 25th day of the 13th month in 2012.
-j is the century, e.g. 20 for 2013
-k is the year of the century, e.g. 13 for 2013.

:Write a program using functions that prompts the user to enter a year (e.g. 2008), month (e.g. 1-12), and day of the month (e.g. 1-31), and then displays the name of the day of the week. For example, for year 2013, month 1, and day of month 25, the day of the week is Friday; the user inputs 2013, 1, and 25 in response to your prompts and then you calculate h which will be 6, and then output Friday. Run the program for five different dates. Testing must include different months including January and February, different centuries, and different years.


As far as my program goes this is what i have so far:

Function module:

def day(q, m, j, k,):
    h =(q+(26)(m+1))/((10)+k+(k/4)+(j/4)+(5*j))%7
    if h==1:
        day="Sunday"
    elif h==2:
        day="Monday"
    elif h==3:
        day="Tuesday"
    elif h==4:
        day="Wednesday"
    elif h==5:
        day="Thursday"
    elif h==6:
        day="Friday"
    else:
        day="Saturday"
    return day



Main module:

import q5_function

q = int (input("enter day of the month"))
m = int (input ("enter the month"))
j = int (input("enter the century"))
k = int (input("enter the year of the century"))
day = q5_function.day(q, m, k, j)
print (day)




This is the error message i get when trying to run the program:

enter day of the month1
enter the month1
enter the century20
enter the year of the century10

Traceback (most recent call last):
File "/Users/nathandavis9752/CP104/davi0030_a4/src/q5_main.py", line 18, in <module>
day = q5_function.day(q, m, k, j)
File "/Users/nathandavis9752/CP104/davi0030_a4/src/q5_function.py", line 13, in day
h =(q+(26)(m+1))/((10)+k+(k/4)+(j/4)+(5*j))%7
TypeError: 'int' object is not callable



Thank you all for your help!

解决方案

the only thing I can see is that in all the python refs I have, it would be

h == 1:




(note the spaces)

Im wondering if the lack of spaces (although most languages don't care) throws the parser off ...


btw, its Zeller's congruence, you've dropped the 'Z'

refs : http://www.tutorialspoint.com/python/python_if_else.htm[^]

http://stackoverflow.com/questions/19371643/python-if-elif-else-statement[^]


这篇关于用函数编写程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆