我正在尝试让程序搜索用户是否创建了帐户。
arrayOfColumnNames <- c(Score, Size, Temperature)
这是我尝试过的,每次使用尚未创建的帐户运行它时,都会给我这个错误
print'\n'
search = raw_input("For which account are you searching: ")
f = shelve.open("passwords.dat")
passwrd = f[search]
entry = passwrd[0]
f.close()
for line in passwrd:
if search in line:
print line
print '\n'
print "I'm sorry we could not find any account related to " + search
print'\n'
我如何检查它并且我没有收到错误,所以我的程序没有结束?
答案 0 :(得分:1)
您发布了错误报告的不同代码。我假设您刚刚将for line in f[search]:
更改为for line in passwrd:
。在这种情况下,您需要先检查文件中是否有search
。你可以把它放在第一位:
if search in f:
for line in f[search]:
...
或者给它一个空的默认值:
for line in f.get(search, []):
...
答案 1 :(得分:0)
一旦用shelve
打开它,如果&#34;搜索&#34;在字典中它被保存为(f),如果它被保存,它会执行if
语句,如果它不是去其他地方。
print '\n'
search = raw_input("For which account are you searching: ")
f = shelve.open("passwords.dat")
if search in f:
account = f[search]
print account
else:
print "I'm sorry we could not find any account related to " + search
print '\n'
f.close()