Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python 3.3中求解方程_Python_Python 3.x_Python 3.3 - Fatal编程技术网

在Python 3.3中求解方程

在Python 3.3中求解方程,python,python-3.x,python-3.3,Python,Python 3.x,Python 3.3,因此,要从10月15日起的任何日期算出一周中的哪一天,你可以使用一个简单的算法。我的问题是,我从文件中读取了日期(例如,2009-06-12),并将公式放入: w = (d + [2.6 * m - 0.2] + Y + [Y / 4] + 5 * C + [C / 4] ) % 7 日期的格式为yyyy mm dd,我的代码如下所示: count = 5 f = open('/Users/student/Desktop/Harry.txt').readlines()[count] Y = f

因此,要从10月15日起的任何日期算出一周中的哪一天,你可以使用一个简单的算法。我的问题是,我从文件中读取了日期(例如,2009-06-12),并将公式放入:

w = (d + [2.6 * m - 0.2] + Y + [Y / 4] + 5 * C + [C / 4] ) % 7
日期的格式为yyyy mm dd,我的代码如下所示:

count = 5
f = open('/Users/student/Desktop/Harry.txt').readlines()[count]
Y = f[2:4]
C = f[:2]
m = f[5:7]
d = f[8:10]
w = (d + [2.6 * m - 0.2] + Y + [Y / 4] + 5 * C + [C / 4] ) % 7
if w == 0:
    print (f, "is a Sunday")
elif w == 1:
    print (f, "is a Monday")
elif w == 2:
    print (f, "is a Tuesday")
elif w == 3:
    print (f, "is a Wednesday")
elif w == 4:
    print (f, "is a Thursday")
elif w == 5:
    print (f, "is a Friday")
elif w == 6:
    print (f, "is a Saturday")
澄清:

w = day of the week counting from Sunday = 0 Monday = 1
d = the day of the month (for e.g. 28th 13th)
m = month number where March = 1 etc.
Y = last 2 digits of year
C = first 2 digits of year
但我得到了这个错误

Traceback (most recent call last):
  File "/Users/student/Documents/workspace/Tutorial Challenges/src/Day_Of_The_Week.py", line 7, in <module>
    w = (d + [2.6 * m - 0.2] + Y + [Y / 4] + 5 * C + [C / 4] ) % 7
TypeError: can't multiply sequence by non-int of type 'float'
回溯(最近一次呼叫最后一次):
文件“/Users/student/Documents/workspace/Tutorial Challenges/src/Day_Of The_Week.py”,第7行,在
w=(d+[2.6*m-0.2]+Y+[Y/4]+5*C+[C/4])%7
TypeError:无法将序列与“float”类型的非int相乘

非常感谢您的帮助。

Y
C
m
d
都是字符串。首先要将它们转换为整数:

Y = int(f[2:4])
C = int(f[:2])
...

不过,你确定这个等式有效吗?它看起来会产生很多非整数工作日。你可能弄错了。此外,括号在Python中不是分组运算符;它们是列表构造语法。您需要在
w
的表达式中用括号替换这些括号。(或者那些括号应该是floor操作符吗?如果是这样,你需要
math.floor
,来自
math
模块,或者只要
int
,如果可以截断的话。)

Y
C
m
d
都是字符串。首先要将它们转换为整数:

Y = int(f[2:4])
C = int(f[:2])
...

不过,你确定这个等式有效吗?它看起来会产生很多非整数工作日。你可能弄错了。此外,括号在Python中不是分组运算符;它们是列表构造语法。您需要在
w
的表达式中用括号替换这些括号。(或者那些括号应该是floor操作符吗?如果是这样的话,你需要
math.floor
,从
math
模块,或者只要
int
,如果可以截断的话。)

正如上面的用户所说,你想要从string转换为int。然而,这是使用datetime模块的理想地方。您可以使用此选项指定日期时间的格式为:yyyy-mm-dd,并使用模块将信息加载为日期时间


正如上面的用户所说,您希望将字符串转换为int。但是,这是使用datetime模块的理想位置。您可以使用此选项指定日期时间的格式为:yyyy-mm-dd,并使用模块将信息加载为日期时间


我试过了,但它不允许,因为我无法将'list'更改为'int'抱歉,在将int添加到Y、C等中并添加()而不是[]之后,它现在可以工作了。我试过了,但它不允许,因为我无法将'list'更改为'int'抱歉,在将int添加到Y、C等并添加()而不是[]之后,它现在可以工作了这是一个好主意,但在这个任务中,它指定我不允许使用datetime模块Ohh,ok。你能在你的问题中添加一些Harry.txt中的示例行吗?这将有助于澄清您正在阅读的内容。这是一个好主意,但在本任务中,它规定我不允许使用datetime模块Ohh,好的。你能在你的问题中添加一些Harry.txt中的示例行吗?这将有助于澄清你在读什么。