python中字典键和值转换的erros

2024-04-24 12:05:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用python为埃及分部编写代码。 什么是埃及分部 例如,17除以3

    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' 

如何删除此错误


Tags: oftheforinputintegerkeysdictlist
1条回答
网友
1楼 · 发布于 2024-04-24 12:05:10

在第二个循环中,将(key, value)元组分配给单个变量keys

for keys in sorted (Egyptian_Division_dict.items(),reverse = True):

然后尝试将该元组添加到整数:

quetient = 0

# in the loop
w = keys 
quetient = quetient + w

抛出异常的是最后一行

您似乎忘记了在该循环中包含values变量;将其重新添加到中至少会使keys再次成为单个键:

for keys, values in sorted (Egyptian_Division_dict.items(),reverse = True):

有了这个变化,quetient的值实际上加起来是15:

>>> Egyptian_Division_dict = {8: 32, 4: 16, 2: 8, 1: 4}
>>> rough = 0
>>> quetient = 0
>>> for keys, values in sorted (Egyptian_Division_dict.items(),reverse = True):
...     w = keys
...     z = values
...     if rough + z <a:
...         rough = rough+z
...         quetient = quetient + w
...     else :
...         print quetient
...
>>> quetient
15

相关问题 更多 >