我正在尝试制作一本简单的电话簿,如果您输入 1:如果将联系人添加到词典中,如果 2您根据输入的名称(键)查找字典,如果 3您根据输入的数字(值)查找字典
当我基于值(输入3)运行键查找时,无论是否为真,它都会返回else函数“ this is invalid”。
有人可以解密吗?
#Input contact name
if button == 1:
name = input('Please enter the contact name:')
if name in contacts:
print("The name you entered already exists in the address book --> %s:%s"\
%(name,contacts[name]))
flag = input("Whether to modify user information (YES/NO):")
if flag== 'YES':
tel = input('Please enter the users contact phone number:')
contacts.update({name:tel}) #Update dictionary
print("Contacts have been updated!")
else:
continue
else:
contacts[name] = input('Please enter the contact phone number:')
print("Contact has been saved!")
#Search by contact name
if button == 2:
name = input('Please enter the contact name:')
if name in contacts:
print("%s : %s "%(name,contacts[name]))
else:
print('The name you entered is no longer in the address book! ')
#Search by contact number
if button == 3:
numba = input('Please enter the contact number:')
lookup = []
for key,value in contacts.items():
if(value == numba):
lookup.append(key)
print('Name(s) matching number is',lookup)
else:
print('This is invalid')
答案 0 :(得分:0)
尝试一下:
class EditUserProfile extends StatefulWidget {
static String id = 'edit-profile';
@override
_EditUserProfileState createState() => _EditUserProfileState();
}
enum Gender { male, female }
class _EditUserProfileState extends State<EditUserProfile> {
Gender _gender = Gender.male;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.white,
iconTheme: IconThemeData(color: Colors.black),
title: Text(
"Edit Profile",
style: TextStyle(color: Colors.black),
),
actions: [
Padding(
padding: const EdgeInsets.all(8.0),
child: RaisedButton(
onPressed: null,
child: Text('Done'),
),
)
],
),
body: ListView(
children: [
InputField(
icon: Icon(
Icons.person,
color: Colors.white,
),
showText: true,
labelText: 'Nickname',
hintText: 'What do they call you?',
),
SizedBox(
height: 10,
),
InputField(
icon: Icon(
Icons.phone_in_talk,
color: Colors.white,
),
showText: true,
labelText: 'Mobile Number',
hintText: 'How can people reach your cell?',
),
SizedBox(
height: 10,
),
InputField(
icon: Icon(
FontAwesomeIcons.child,
color: Colors.white,
),
showText: true,
labelText: 'Age',
hintText: 'How old are you?',
),
SizedBox(
height: 10,
),
InputField(
icon: Icon(
Icons.person,
color: Colors.white,
),
showText: true,
labelText: 'Gender',
hintText: 'Are you single or married?',
),
SizedBox(
height: 10,
),
IntrinsicHeight(
child: Row(
crossAxisAlignment: CrossAxisAlignment.stretch,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Flexible(
child: RadioListTile<Gender>(
title: Text('Male'),
value: Gender.male,
groupValue: _gender,
onChanged: (Gender value) {
setState(() {
_gender = value;
});
},
),
),
Flexible(
child: RadioListTile<Gender>(
title: Text('Female'),
value: Gender.female,
groupValue: _gender,
onChanged: (Gender value) {
setState(() {
_gender = value;
});
},
),
),
],
),
),
InputField(
icon: Icon(
FontAwesomeIcons.locationArrow,
color: Colors.white,
),
showText: true,
labelText: 'Lives In',
hintText: 'Where do you reside',
),
SizedBox(
height: 10,
),
InputField(
icon: Icon(
Icons.person,
color: Colors.white,
),
showText: true,
labelText: 'Relationship Status',
hintText: 'Are you single or married?',
),
SizedBox(
height: 10,
),
],
),
);
}
}
答案 1 :(得分:0)
可能有点麻烦,但是我做到了:
numba = input('Please enter the contact number: ')
lookup = []
for key,value in contacts.items():
if(numba == key):
lookup.append(value)
print('Name(s) matching number is', lookup)
if(int(numba) > len(contacts.items())):
print('This is invalid')