Python 在if语句中不能调用布尔值
我的代码有问题,因为我想做一个例外,而If语句给了我一个错误(Python 在if语句中不能调用布尔值,python,python-3.x,Python,Python 3.x,我的代码有问题,因为我想做一个例外,而If语句给了我一个错误(If-allow\u-name(True))。我的代码有什么问题 我的代码: try: dates = soup.find('div', class_='row no-gutters align-items-center').find('time') _dates.append(dates['datetime']) except: await ctx.send(f'This name is unavailab
If-allow\u-name(True)
)。我的代码有什么问题
我的代码:
try:
dates = soup.find('div', class_='row no-gutters align-items-center').find('time')
_dates.append(dates['datetime'])
except:
await ctx.send(f'This name is unavailable')
print('This name is unavailable')
allow_name = False
if allow_name(True):
users = soup.find('div', class_='col-md-7').find('samp')
for user in users:
names.append(user)
for date in _dates:
date = date[0:19]
date = datetime.datetime.strptime(date, '%Y-%m-%dT%H:%M:%S')
unix = date.timestamp()
unix = f'{unix}'
unix = unix[0:10]
unix = int(unix)
unix = unix + 7200
dates2.append(unix)
你应该只使用:
if allow_name:
所以你的代码应该是这样的:
if allow_name:
users = soup.find('div', class_='col-md-7').find('samp')
for user in users:
names.append(user)
...
python非常聪明,您不应该使用variable==True,因为如果它将为False,if语句将获取它并移动到False条件如果有一个名为allow_name的函数,则在启动变量“allow name”时会覆盖它 更改变量名,或者如果您只是想检查变量“allow_name”是否为true或false,请使用:
if allow_name: #True
....
else: #False
....
如果allow\u name==True,则要调用
。allow\u name
是返回布尔值还是接受布尔值参数?它应该接受一个参数并检查它是否为True。因此,如果它有一个异常,它不会在没有第一个列表的情况下编写它