当我尝试搜索列表时收到 TypeError: cannot unpack non-iterable bool object
。当我返回一个布尔值和我正在搜索的项目的索引值时。
def name_ser(name):
found = False
for i in range(len(names)):
if names[i] == name:
found = True
return found, names.index(name)
else:
found = False
return found,None
def main_menu():
print('Welcome!\nPlease Choose from the following options...')
print('1: Create an account\n2: Login ')
opt = int(input('Enter Your Choice: '))
if opt == 1:
name_search = input('Enter Name... ')
found, _ = name_ser(name_search)
if found == True:
print("Account Already excites!")
elif found == False & len(names) == 0:
acc_creation(name_search)
print('Account created!')
错误:
Traceback (most recent call last):
File "/Users/darkmbs/VS-Code/FirstPythonProject/accounts.py", line 100, in <module>
main_menu()
File "/Users/darkmbs/VS-Code/FirstPythonProject/accounts.py", line 77, in main_menu
found, _ = name_ser(name_search)
TypeError: cannot unpack non-iterable NoneType object
答案 0 :(得分:1)
import React from 'react';
import { Header } from 'components/header/Header';
import { SEO } from 'components/seo/SEO';
import { SliceZone } from 'components/slice-zone/SliceZone';
import { client } from 'lib/prismic';
import { RichText } from 'prismic-reactjs';
const Index = ({ document }: any) => {
const slices = document;
if (!slices) return null;
return (
<>
<SEO
title={RichText.asText(document.page_title)}
description={document.meta_description}
banner={document.social_image.url}
/>
<SliceZone slices={slices} />
</>
);
};
export const getServerSideProps = async () => {
const options = {
lang: 'en-us',
};
const { data: document } = await client.getSingle('homepage', options);
return {
props: {
document,
},
};
};
export default Index;
可以返回 3 个不同的对象。
name_ser
您需要一致的返回类型。但实际上,这个功能根本不应该存在。它试图返回 def name_ser(name):
found = False
for i in range(len(names)):
if names[i] == name:
found = True
return found, names.index(name) <== returns 2-tuple
else: <== if name[0] doesn't match,
found = False executes immediately
return found <== returns boolean
<== if names is empty, returns None
或 name
中 names
的索引。 False
差不多就是这样。
str.find
在添加帐户之前,我还删除了 def main_menu():
print('Welcome!\nPlease Choose from the following options...')
print('1: Create an account\n2: Login ')
opt = int(input('Enter Your Choice: '))
if opt == 1:
name_search = input('Enter Name... ')
if name_search in names:
print("Account Already exists!")
else:
acc_creation(name_search)
print('Account created!')
为空的检查。我觉得如果只有一个人可以创建一个帐户会很奇怪。
答案 1 :(得分:1)
正如之前的一些评论中提到的,您不能同时返回一个或两个值。我的建议是返回名称的索引。或者,如果未找到,则返回值 -1
。
def name_ser(name):
# Check if element is in list
if name in names:
# Return index of element
return names.index(name)
else:
# Name was not found in the list
return -1
def main_menu():
print('Welcome!\nPlease Choose from the following options...')
print('1: Create an account\n2: Login ')
opt = int(input('Enter Your Choice: '))
# Create account
if opt == 1:
name_search = input('Enter Name... ')
found = name_ser(name_search)
if found >= 0:
print("Account Already exists!")
elif found == -1 & len(names) == 0:
acc_creation(name_search)
print('Account created!')
# Login to Account
if opt == 2:
# Update to use index instead of boolean