gpt4 book ai didi

python - 代码将整数读取为 float

转载 作者:行者123 更新时间:2023-12-01 09:10:43 27 4
gpt4 key购买 nike

我的代码将 iMid 读取为 float 并给出 TypeError,即使将其包装在整数函数中也是如此。另外,是否还有另一种方法来查找中间值的索引,这比我在这里尝试的方法更容易?

def isIn(char, aStr):
'''
char: a single character
aStr: an alphabetized string

returns: True if char is in aStr; False otherwise
'''
# Your code here
import numpy as np
def iMid(x):
'''
x : a string

returns: index of the middle value of the string

'''

if len(x) % 2 == 0:
return int(np.mean(len(x)/2, (len(x)+2)/2)) #wrapped the
# answer for iMid
#in the integer function
else:
return int((len(x)+1)/2)

if char == aStr[iMid] or char == aStr: #iMid is not being interpreted as an integer
return True
elif char < aStr[iMid]:
return isIn(char, aStr[0:aStr[iMid]])
else:
return isIn(char, aStr[aStr[iMid]:])

print(isIn('c', "abcd"))

最佳答案

if char == aStr[iMid] or char == aStr: #iMid is not being interpreted as an integer

iMid 不是整数。这就是函数。

您需要调用该函数来获取它返回的整数。

if char == aStr[iMid(aStr)] or char == aStr: #iMid is called and returns an integer

关于python - 代码将整数读取为 float ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51669178/

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