在python项目中引入elif或else时遇到问题

2024-09-28 18:17:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我让我的程序按照我希望的方式运行,直到我引入了当前货币的替代方案美元。如果currentCurrency是CAD,我希望它运行第一组if语句,但是如果currentCurrency是USD,则运行下一组if语句。我对python是新手,似乎无法在介绍else或elif后立即让它工作。我读过其他类似的问题,也看过youtube视频,但似乎仍然看不出我错在哪里。我感谢你的帮助

CAD_USD = 0.9785
CAD_EUR = 0.7935
CAD_GBP = 0.6387
CAD_YEN = 79.9794
CAD_CNY = 6.4295
CAD_CHF = 0.9614
CAD_INR = 53.8968

#Value of USD currency to other currencies
USD_CAD = 1.0220
USD_EUR = 0.7764
USD_GBP = 0.6250
USD_YEN = 78.2584
USD_CNY = 6.2895
USD_CHF = 0.9407
USD_INR = 52.7345

#Value of Eur currency to other currencies
EUR_CAD = 1.2602
EUR_USD = 1.28779
EUR_GBP = 0.8049
EUR_YEN = 100.7700
EUR_CNY = 8.0987
EUR_CHF = 1.2113
EUR_INR = 67.9060
#Value of GBP to other currencies
GBP_CAD = 1.565
GBP_USD = 1.6000
GBP_EUR = 1.2424
GBP_YEN = 125.2000
GBP_CNY = 10.0611
GBP_CHF = 1.5052
GBP_INR = 84.3593
#Prompts user for data to work with for conversion

currentCurrency= input("Please enter currency in hand:")
requiredCurrency= input("Please enter currency required:")
currentAmount =  float (input ("Please enter amount(currency value):"))

#Converting CAD currency

if currentCurrency == "CAD" or "cad":
    if requiredCurrency == "USD" or "usd":
        conversion= currentAmount*CAD_USD
        if currentCurrency == "CAD" or "cad":
            if requiredCurrency == "EUR" or "eur":
                conversion= currentAmount*CAD_EUR
                if currentCurrency == "CAD" or "cad":
                    if requiredCurrency == "GBP" or "gbp":
                        conversion= currentAmount*CAD_GBP
                        if currentCurrency == "CAD" or "cad":
                            if requiredCurrency == "YEN" or "yen":
                                conversion= currentAmount*CAD_YEN
                                if currentCurrency == "CAD" or "cad":
                                    if requiredCurrency == "CNY" or "cny":
                                        conversion= currentAmount*CAD_CNY
                                        if currentCurrency == "CAD" or "cad":
                                            if requiredCurrency == "CHF" or "chf":
                                                conversion= currentAmount*CAD_CHF
                                                if currentCurrency == "CAD" or "cad":
                                                    if requiredCurrency == "INR" or "inr":
                                                        conversion= currentAmount*CAD_INR
                                                        a1 = round(conversion)    
                                                        print(currentAmount, " in CAD is equivalent to",a1," in", requiredCurrency)
                                                        elif currentCurrency == "USD" or "usd":
                                                            else requiredCurrency == "CAD" or "cad":
                                                                conversion= currentAmount*USD_CAD
                                                                if currentCurrency == "USD" or "usd":
                                                                    if requiredCurrency == "EUR" or "eur":
                                                                        conversion= currentAmount*USD_EUR
                                                                        if currentCurrency == "USD" or "usd":
                                                                            if requiredCurrency == "GBP" or "gbp":
                                                                                conversion= currentAmount*USD_GBP
                                                                                if currentCurrency == "USD" or "usd":
                                                                                    if requiredCurrency == "YEN" or "yen":
                                                                                        conversion= currentAmount*USD_YEN
                                                                                        if currentCurrency == "USD" or "usd":
                                                                                            if requiredCurrency == "CNY" or "cny":
                                                                                                conversion= currentAmount*USD_CNY
                                                                                                if currentCurrency == "USD" or "usd":
                                                                                                    if requiredCurrency == "CHF" or "chf":
                                                                                                        conversion= currentAmount*USD_CHF
                                                                                                        if currentCurrency == "USD" or "usd":
                                                                                                            if requiredCurrency == "INR" or "inr":
                                                                                                                conversion= currentAmount*USD_INR
                                                                                                                a1 = round(conversion)    
                                                                                                                print(currentAmount, " in USD is equivalent to",a1," in", requiredCurrency)

Tags: orifeurusdgbpcadconversioninr
1条回答
网友
1楼 · 发布于 2024-09-28 18:17:06

下面是一个示例,说明了在currentCurrency == "CAD"情况下如何构造分支:

if currentCurrency == "CAD":
    if requiredCurrency == "USD":
        pass
    elif requiredCurrency == "EUR":
        pass
    elif requiredCurrency == "GBP":
        pass
    elif requiredCurrency == "YEN":
        pass
    elif requiredCurrency == "CNY":
        pass
    elif requiredCurrency == "CHF":
        pass
    elif requiredCurrency == "INR":
        pass

注意,如果currentCurrency != "CAD",则跳过整个块。这是您通常想要的行为,称为“短路”。本质上,您希望尽快选择逻辑分支,以避免检查不必要/冗余的情况

还要注意,这些是单一条件:if currentCurrency == "CAD"。您可以通过利用str.upper()函数来实现这一点,该函数返回字符串的全大写版本,并对输入字符串调用此方法:

currentCurrency= input("Please enter currency in hand:").upper()
requiredCurrency= input("Please enter currency required:").upper()

相关问题 更多 >