0
我正在爲Python中的埃及分部編寫代碼。 什麼是埃及分部 例如,通過17 3.erros與python中的字典鍵和值轉換
Powers of two column divisor doubling column
2^0 = 1 3
2^1 = 2 6
2^2 = 4 12
2^3 = 8 24
24>17 so we can stop.
分縱觀3,6的組合,和12我們看到,12 + 3 = 15是 我們似乎可以得到不最接近超過17.
現在我得到了上面的輸出,但我無法得到商,因爲我使用字典,它給了我一個錯誤。
import sys
astring = raw_input("Enter the A integer: ") #accepting input
a = int(astring) #dividend
bstring = raw_input("Enter the B integer: ") # accepting input
b = int(bstring) #divisor
Egyptian_Division_dict = {} # initiating dictionary
i =0 # setting counter for 2^i
x =0
z =0
#k = 0 #m = 0
list_of_divisor = []
list_of_two = []
while i< a:
k = 2**i
m = k*b
if m < a:
list_of_divisor.append (k)
list_of_two.append(m)
i += 1
for i in range(len(list_of_divisor)):
Egyptian_Division_dict[list_of_divisor[i]] = list_of_two[i]
list_of_divisor = sorted(list_of_divisor, reverse=True)
list_of_two = sorted(list_of_two, reverse=True)
print "printing dictionary"
for keys,values in sorted (Egyptian_Division_dict.items(),reverse = True):
print (keys), (values)
#if z == a:
# z=+value
#print z
rough = 0
quetient = 0
for keys in sorted (Egyptian_Division_dict.items(),reverse = True):
#Egyptian_Division_dict[keys] = str([keys])
#Egyptian_Division_dict[values] = str([values])
w = keys
z = values
if rough + z <a:
rough = rough+z
quetient = quetient + w
else :
print quetient
錯誤:
Enter the A integer: 44
Enter the B integer: 4
printing dictionary
8 32
4 16
2 8
1 4
Traceback (most recent call last):
File "main.py", line 39, in <module>
quetient = quetient + w
TypeError: unsupported operand type(s) for +: 'int' and 'tuple'
如何刪除這個錯誤?
哦非常感謝你。錯誤被刪除,我的代碼運行正常。 –