def compute(exp):
o1 = ''
o2 = ''
for c in exp:
if c != '-':
if not '-' in list(exp)[:list(exp).index(c)]:
o1 = o1 + c
else:
o2 = o2 + c
print(o1)
print(o2)
return int(o1)-int(o2)
if __name__ == '__main__':
exp = input('-->')
result = compute(exp)
結果:
-->1-1
11
Traceback (most recent call last):
File "x.py", line 16, in <module>
result = compute(exp)
File "x.py", line 12, in compute
return int(o1)-int(o2)
ValueError: invalid literal for int() with base 10: ''
你的代碼看得出來 Python 還沒熟練, 不過沒關係, 我們先從這個問題下手, 我稍微測試了一下你的 code:
結果:
你發現了嗎? o1 跟 o2 收集的不對
先丟三個小提示:
string 不必先轉成 list 才能問
in
(if b in A
就可以了)print(int(o1)-int(o2))
應該要拿到外一層 (收集完畢才運算)lst.index(x)
總是返回lst
中第一個x
出現的位置我回答過的問題: Python-QA