這是我家庭作業的一部分,我接近最終答案但還不完全。我需要編寫一個函數,將位置1和5之間的奇數寫入列表中。 我做這樣的事情: -in我寫奇數條件的文件域名:在列表中編寫奇數python
def oddNumber(x):
"""
this instruction help us to write the odd numbers from the positions specificated
input: x-number
output:-True if the number is odd
-False otherwise
"""
if x % 2==1:
return True
else:
return False
- 然後測試:
def testOdd_Number():
testOdd_Number=[0,1,2,3,4,5,6,7,8]
oddNumber(testOdd_Number,0,6)
assert (testOdd_Number==[1,3,5])
oddNumber(testOdd_Number,0,3)
assert (testOdd_Number==[3])
- 和在其他文件中命名用戶接口我寫的這樣的:
elif(cmd.startswith("odd from ", "")):
try:
cmd=cmd.replace("odd from ", "")
cmd=cmd.replace("to ", "")
i=int(cmd[:cmd.find(" ")])
j=int(cmd[cmd.find(" "):])
if (i>=len(NumberList) or i>j or j>=len(NumberList) or i<0 or j<0):
print("Invalid value(s).")
else:
for m in range(i-1,j):
if oddNumber(NumberList[m]):
print (NumberList[m])
except:
print("Error!")
- 當我運行整個項目(我有更多的要求,但其他人一個好),和寫入[POS]至[POS]奇怪,它說米Ë
Traceback (most recent call last):
File "C:\Users\Adina\My Documents\LiClipse Workspace\P1\userinterface.py", line 94, in <module>
run()
File "C:\Users\Adina\My Documents\LiClipse Workspace\P1\userinterface.py", line 77, in run
elif(cmd.startswith("odd from ", "")):
TypeError: slice indices must be integers or None or have an __index__ method
我忘了說,我有一個也是一個main()函數,我打印requirements.Where我錯了?
'cmd [:cmd.find(「」)]'如果在'cmd'中找不到''''''會出錯。 – khelwood 2014-10-26 14:04:23