gpt4 book ai didi

python - 类型错误 : unsupported operand type(s) for div: 'str' and 'int' [line 14]

转载 作者:行者123 更新时间:2023-12-01 03:35:21 26 4
gpt4 key购买 nike

我正在为 google foobar 创建一个函数,它计算列表中某个字符的数量,它会产生此错误,

TypeError: unsupported operand type(s) for div: 'str' and 'int' [line 14]

这是代码

def answer(s):
sl2 = []
ol = '10'
if(len(s) != 0):
if(len(s) > 200):
print("That is too long")
else:
s = " ".join(s[i:i+1] for i in range(0, len(s), 1))
sl = s.split()
sl_a = sl.count('a')
sl_b = sl.count('b')
sl_c = sl.count('c')
smallest_int = min(sl_a, sl_b, sl_c)
final_amount = ol / smallest_int #line 14 (the problem)
print(final_amount)

answer(raw_input('Describe the M&Ms'))

我不知道这是否只是 google foobar 编译器的问题还是我有限的 python 知识。

最佳答案

问题是您试图将 str 类型值除以 int 类型值。在进行除法运算之前将 ol 转换为 int 类型,或者将 ol 设置为 10 而不是 '10'

def answer(s):
sl2 = []
ol = 10
if(len(s) != 0):
if(len(s) > 200):
print("That is too long")
else:
s = " ".join(s[i:i+1] for i in range(0, len(s), 1))
sl = s.split()
sl_a = sl.count('a')
sl_b = sl.count('b')
sl_c = sl.count('c')
smallest_int = min(sl_a, sl_b, sl_c)
final_amount = ol / smallest_int #line 14 (the problem)
print(final_amount)

answer(raw_input('Describe the M&Ms'))

注意:- 顺便说一下,在 javascript 中,这个东西是有效的,javascript 在使用除法运算之前会隐式地将 '10' 转换为 10。但是,python 不会允许你这样做。

关于python - 类型错误 : unsupported operand type(s) for div: 'str' and 'int' [line 14],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40436346/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com