Python循环无法正确遍历字典

Python循环无法正确遍历字典,python,python-3.x,loops,dictionary,for-loop,Python,Python 3.x,Loops,Dictionary,For Loop,我正在为我的个人理财项目工作,我试图让用户能够选择他们希望交易的货币。我试图进行错误处理,如果他们输入的键在字典中不存在,它会显示一条消息并关闭程序。但现在,即使我输入了正确的键,它仍然会关闭 print("Currency's supported: USD, EUR, CAN, YEN, GBP") currencyCheck = input("Input the currency you would like to use. Ex: 'USD' or 'EUR'...etc ").upper

我正在为我的个人理财项目工作,我试图让用户能够选择他们希望交易的货币。我试图进行错误处理,如果他们输入的键在字典中不存在,它会显示一条消息并关闭程序。但现在,即使我输入了正确的键,它仍然会关闭

print("Currency's supported: USD, EUR, CAN, YEN, GBP")
currencyCheck = input("Input the currency you would like to use. Ex: 'USD' or 'EUR'...etc ").upper()


#currencySYM is a dictionary of currency ticker symbols and currency symbols
currencySYM = {'USD':'$', 'EUR':'€', 'CAN':'C$','YEN':'¥','GBP':'£'}
#the for loop takes the input from Currencycheck and applies the correct symbol to the letters
for key in currencySYM:
    if currencyCheck == key:
        currencyCheck = currencySYM[key]

    elif currencyCheck != key:
        print("Make sure you type the correct three letter symbol.")
        exit()

如果我取出else语句,它可以工作,但不符合预期,我可以键入任何单词,它不必是键,它会将它分配给变量,甚至不检查它是否作为键存在于字典中

在检查第一个键后,您不想立即中断。一个解决办法可能是

found = False

for key in currencySYM:
    if currencyCheck == key:
        currencyCheck = currencySYM[key]
        found = True

if !found:
    print("Make sure you type the correct three letter symbol.")
    exit()
当然,更好的方法是检查键是否在字典中:

if currencyCheck in currencySYM:
    currencyCheck = currencySYM[key]
else:
    print("Make sure you type the correct three letter symbol.")
    exit()


您正在检查字典中的每个键是否都已输入,这是不可能的。您可以通过在
if
子句末尾添加
break
语句轻松解决此问题:

 if currencyCheck == key:
    currencyCheck = currencySYM[key]
    # found the key, so stop checking
    break
在任何情况下,此代码都不是很pythonic,您可以用整个循环替换此代码,如果dict中没有键,则将返回None:

currencyCheck = currencySYM.get(key,None)
if not currencyCheck:
    print("Make sure you type the correct three letter symbol.")
    exit()

如果要检查给定词典中是否存在
,可以在dict中执行此操作

currencyCheck = input("Input the currency you would like to use. Ex: 'USD' or 'EUR'...etc ").upper()
currencySYM = {'USD':'$', 'EUR':'€', 'CAN':'C$','YEN':'¥','GBP':'£'}
if currenyCheck in currencySYM:
    currencyCheck = currencySYM[key]
else:
    exit()
或者您可以使用
.get()

字典是无序的-由于字典中的值由键索引,因此它们不按任何特定顺序保存,这与列表不同,列表中的每个项都可以根据其在列表中的位置进行定位

这应该是可行的:

currencyCheck = input("Input the currency you would like to use. Ex: 'USD' or 'EUR'...etc ").upper()

#currencySYM is a dictionary of currency ticker symbols and currency symbols
currencySYM = {'USD':'$', 'EUR':'€', 'CAN':'C$','YEN':'¥','GBP':'£'}
#the for loop takes the input from Currencycheck and applies the correct symbol to the letters
key_list = currencySYM.keys()
if currencyCheck in key_list:
    currencyCheck = currencySYM[currencyCheck]

elif currencyCheck not in key_list:
    print("Make sure you type the correct three letter symbol.")
    exit()

为什么会存在这个循环<代码>如果输入currencySYM:…您在第一项不匹配后退出。如果没有匹配的项,您希望退出,但这里也不需要循环。变量和函数名称应遵循带有下划线的
小写形式。另外,您使用了
exit()
,但我认为您可能一直在寻找
break
。谢谢!,我想我应该读一下pythoniccode的指南,我想我只是假设我需要一个字典循环,谢谢@WillArmentrout您想要使用循环的解决方案吗?
currencyCheck = input("Input the currency you would like to use. Ex: 'USD' or 'EUR'...etc ").upper()

#currencySYM is a dictionary of currency ticker symbols and currency symbols
currencySYM = {'USD':'$', 'EUR':'€', 'CAN':'C$','YEN':'¥','GBP':'£'}
#the for loop takes the input from Currencycheck and applies the correct symbol to the letters
key_list = currencySYM.keys()
if currencyCheck in key_list:
    currencyCheck = currencySYM[currencyCheck]

elif currencyCheck not in key_list:
    print("Make sure you type the correct three letter symbol.")
    exit()