我尝试使用实现为字典的数据库来搜索用户输入的值,如果找到该值,则会显示该值,否则会显示错误消息。 ' UUC'或者' uum'等等是指大学校园的缩写。
用户应该能够输入上述值之一,并且应该显示任何匹配项。但是,这在我的代码中不起作用,我无法找到问题。
def all_from(datab,university):
l = len(datab)
k = 1
i = 0
while k <= 1:
s = datab[k]
the_university = s[2]
if university == the_university:
i = i + 1
print datab[k]
k = k + 1
if i == 0:
print 'Nobody goes to University there!'
datab = dict()
datab[1] = ['walker','matthew','uuc',1]
datab[2] = ['stewart','rory','uum',2]
datab[3] = ['toner','kevin','qub',4]
datab[4] = ['hughes','johnny','uuj',1]
datab[5] = ['douglas','kenny','uuc', 3]
datab[6] = ['hooks', 'peter','qub',1]
datab[7] = ['campbell','vicky','uuj',2]
datab[8] = ['crooks','laura','uum',4]
datab[9] = ['irwin','emma','uuc',3]
datab[10] = ['patterson','steve','uuc',1]
university = (raw_input('Enter the University here: '))
all_from(datab,university)
答案 0 :(得分:1)
您可以简化 public class Player : INotifyPropertyChanged
{
protected void OnPropertyChanged([CallerMemberName] string caller = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(caller));
}
public event PropertyChangedEventHandler PropertyChanged;
public Player( int high)
{
HighScore = high;
}
private int _highScore;
public int HighScore
{
get { return _highScore; }
set
{
_highScore = value;
OnPropertyChanged();
}
}
}
功能:
public class AllPlayers : INotifyPropertyChanged
{
public ObservableCollection<Player> Players { get; set; }
public AllPlayers()
{
Players = new ObservableCollection<Player>();
}
protected void OnPropertyChanged([CallerMemberName] string caller = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(caller));
}
public event PropertyChangedEventHandler PropertyChanged;
public void ChangeScore(int playerIndex, int highScore)
{
Players[playerIndex].HighScore = highScore;
}
private void UpdateBiggestScore()
{
BiggestScore = (from player in Players select player.HighScore).Max();
}
private int _biggestScore;
public int BiggestScore
{
get { return _biggestScore; }
set
{
_biggestScore = value;
OnPropertyChanged();
}
}
}
答案 1 :(得分:1)
我的贡献。我觉得将学生安排为列表中的项目,将不同的字段作为字典键是一种更好的数据结构。
def find_all(datab,uni):
for student in datab:
if student['uni'] == uni:
print(student)
datab = []
datab.append( {'lastname':'walker', 'firstname':'matthew','uni':'uuc','year':1})
datab.append( {'lastname':'stewart','firstname':'rory','uni':'uum','year':2})
datab.append( {'lastname':'toner','firstname':'kevin','uni':'qub','year':4})
datab.append( {'lastname':'hughes','firstname':'johnny','uni':'uuj','year':1})
uni = input('Enter the uni here: ')
find_all(datab,uni)
如果您想搜索所有密钥,那么您可以更改
if student['uni'] == uni
行到
for key, value in student.items():
if value == uni:
另一个版本:这将允许您输入一个非常简单的查询。
def find_all(datab,query):
query = query.split('OR')
for search_terms in query:
for student in datab:
for key, value in student.items():
if value == search_terms.strip():
print(student)
datab = list()
datab.append( {'lastname':'walker', 'firstname':'matthew','uni':'uuc','year':'1'})
datab.append( {'lastname':'stewart','firstname':'rory','uni':'uum','year':'2'})
datab.append( {'lastname':'toner','firstname':'kevin','uni':'qub','year':'4'})
datab.append( {'lastname':'hughes','firstname':'johnny','uni':'uuj','year':'1'})
query = input('Enter your query here: ')
find_all(datab,query)
这是我的代码运行两个简单的查询
Enter the uni here: uum OR uuc
{'firstname': 'rory', 'lastname': 'stewart', 'uni': 'uum', 'year': 2}
{'firstname': 'matthew', 'lastname': 'walker', 'uni': 'uuc', 'year': 1}
Enter your query here: 1 OR UUM
{'firstname': 'matthew', 'lastname': 'walker', 'uni': 'uuc', 'year': '1'}
{'firstname': 'johnny', 'lastname': 'hughes', 'uni': 'uuj', 'year': '1'}
您只需在项目之间添加单词OR,它将搜索词典中包含任何单词的任何项目。注:我不得不将uni年改成字符串以使其正常工作。
正如我所说。使用像SQLite这样的适当数据库会更好。