【发布时间】:2014-12-10 21:48:04
【问题描述】:
我正在尝试让我的代码通过并在完成一些数学运算后弹出。被求和的文件只是单独行上的数字列表。你能给我一些指导来完成这项工作吗,因为我很难过。
编辑: 我正在尝试使从 main 函数到 Checker 函数的转换正常工作。我还需要一些切片方面的帮助。从文件中导入的数字是这样的:
136895201785
155616717815
164615189165
100175288051
254871145153
所以在我的Checker 函数中,我想将奇数从左到右相加。例如,对于第一个数字,我想添加 1、6、9、2、1 和 8。
完整代码:
def checker(line):
flag == False
odds = line[1 + 3+ 5+ 9+ 11]
part2 = odds * 3
evens = part2 + line[2 + 4 +6 +8 +10 +12]
part3 = evens * mod10
last = part3 - 10
if last == line[-1]:
return flag == True
def main():
iven = input("what is the file name ")
with open(iven) as f:
for line in f:
line = line.strip()
if len(line) > 60:
print("line is too long")
elif len(line) < 10:
print("line is too short")
elif not line.isdigit():
print("contains a non-digit")
elif check(line) == False:
print(line, "error")
【问题讨论】:
-
你能修复缩进错误吗?
标签: python list function if-statement