【问题标题】:Python loop wont iterate through dictionary correctlyPython循环不会正确遍历字典
【发布时间】:2020-01-22 18:50:56
【问题描述】:

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

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 语句,它可以工作但不是按预期的那样,我可以键入任何单词,它不必是键,它会将它分配给变量,甚至不检查它是否作为键存在字典

【问题讨论】:

  • 为什么会存在这个循环? if key in currencySYM: ....
  • 您在第一项不匹配后退出。如果根本没有匹配项,您想退出,但这里也不需要循环。
  • 变量和函数名称应遵循lower_case_with_underscores 样式。另外,您使用了exit(),但我认为您可能一直在寻找break

标签: python python-3.x loops dictionary for-loop


【解决方案1】:

你不想在检查第一把钥匙后立即爆发。一个修复可能是

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()

【讨论】:

    【解决方案2】:

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

     if currencyCheck == key:
        currencyCheck = currencySYM[key]
        # found the key, so stop checking
        break
    

    无论如何,这段代码不是很pythonic,你可以用整个循环替换这段代码,如果字典中的键不可用,它将返回None:

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

    【讨论】:

    • 谢谢!,我想我应该阅读pythonic代码指南
    【解决方案3】:

    如果您想检查给定字典中是否存在key,您可以执行此操作key in 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 = {'USD':'$', 'EUR':'€', 'CAN':'C$','YEN':'¥','GBP':'£'}
    
    currencyCheck=currencySYM.get(currencyCheck,exit())
    

    字典是无序的——因为字典中的是由键索引的 strong>,它们没有任何特定的顺序,不像列表,每个项目都可以通过它在列表中的位置来定位。

    【讨论】:

    • 我想我只是假设我需要一个字典循环,谢谢!
    • @WillArmentrout 你想要一个使用循环的解决方案?
    【解决方案4】:

    应该可以的:

    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()
    

    【讨论】:

      猜你喜欢
      • 2023-03-12
      • 1970-01-01
      • 2022-09-22
      • 2013-11-29
      • 2020-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多