Home > Software engineering >  Having trouble when introducing elif or else with python project
Having trouble when introducing elif or else with python project

Time:10-16

I got my program to work how I want it to until I introduce the alternative for currentCurrency being USD. I want it to run through the first set of if statements if it is CAD for currentCurrency but run through the next set of if statements if currentCurrency is USD, I am new to python and can't seem to get it to work as soon as I introduce the else or elif. I have read through other similar questions and watched youtube videos but still cant seem to see where I am going wrong. I appreciate the help.

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)

CodePudding user response:

Here's an example of how you could structure a branch for the case when 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

Notice that if currentCurrency != "CAD" this entire block is skipped over. This is the behavior you generally want, and is known as "short-circuiting". In essence, you want to choose a logic branch as soon as possible to avoid checking unnecessary/redundant cases.

Notice also that these are single conditions: if currentCurrency == "CAD". You can do this by taking advantage of the str.upper() function, which returns an all-caps version of the string, and calling this method on the input strings:

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