我的货币转换器应用程序有问题

时间:2021-05-15 17:46:38

标签: python python-3.x

所以我试图制作一个货币转换器应用程序。它询问用户的金额,并询问输入的金额是 PKR(巴基斯坦卢比)还是 USD(美元),并给出是否转换为印度卢比、人民币或欧元的选项。然后将金额转换为所选货币。我遇到的问题是,当我选择将 pkr 转换为其他货币时,它也会转换美元,如果我选择转换美元,它也会转换 pkr。如果代码难以阅读,我很抱歉,我是 Python 初学者。


print ("""Welcome to currency converter!
You can use this application to convert USD and PKR to other major currencies.
You can currently convert to Indian Rupees, China's Yuan, and Euro. More currencies will be hopefully added in the future!

""")

user_amount = input ("Enter the amount that you would like to convert: ")
user_usd_pkr = str(input ("Is the amount entered by you in PKR or USD? Use the letter 'P' for PKR and the letter 'U' for USD:  "))


# This section covers the conversion of PKR to other currencies.

if user_usd_pkr == "P" or user_usd_pkr == "p":
    user_convert_choice = input("""Would you like to convert to -
    
    1. Indian Rupees. (use letter I)
    2. China's Yuan.  (use letter C)
    3. Euro.          (use letter E)
    
    """)
    if user_convert_choice == "I" or user_convert_choice == "i":
     indian_rupees = int(user_amount) * 0.48171
     print (f"{user_amount} PKR is {indian_rupees} Rs.")
    elif user_convert_choice == "C" or user_convert_choice =="c":
        china_yuan = int(user_amount) * 0.042300
        print (f"{user_amount} PKR is {china_yuan} Yuan.")
    elif user_convert_choice == "E" or user_convert_choice =="e":
        euro = int(user_amount) * 0.0054100
        print (f"{user_amount} PKR is {euro} Euro.")
    else:
        print ("You have not entered a valid choice. The program will restart and enter a valid choice this time.")

# This section covers the conversion of USD to other currencies.


elif user_usd_pkr == "U" or user_usd_pkr == "u":
    user_convert_choice = input("""Would you like to convert to - 
    
    1. Indian Rupees. (use letter I)
    2. China's Yuan.   (use letter C)
    3. Euro.          (use letter E)
    
    """)

if user_convert_choice == "I" or user_convert_choice == "i":
    indian_rupees = int(user_amount) * 73.27
    print (f"{user_amount} USD is {indian_rupees} Rs.")
elif user_convert_choice == "C" or user_convert_choice == "c":
    china_yuan = int(user_amount) * 6.44
    print (f"{user_amount} USD is {china_yuan} Yuan.")
elif user_convert_choice == "E" or user_convert_choice == "e":
    euro = int(user_amount) * 0.82
    print (f"{user_amount} USD is {euro} Euro.")

1 个答案:

答案 0 :(得分:1)

问题是缩进。 最低的 if-elif-elif 块不在 elif user_usd_pkr == "U" 块内。

这是一个更正的版本:


print("""Welcome to currency converter!
You can use this application to convert USD and PKR to other major currencies.
You can currently convert to Indian Rupees, China's Yuan, and Euro. More currencies will be hopefully added in the future!

""")

user_amount = input("Enter the amount that you would like to convert: ")
user_usd_pkr = str(input(
    "Is the amount entered by you in PKR or USD? Use the letter 'P' for PKR and the letter 'U' for USD:  "))


# This section covers the conversion of PKR to other currencies.

if user_usd_pkr.upper() == "P":
    user_convert_choice = input("""Would you like to convert to -

    1. Indian Rupees. (use letter I)
    2. China's Yuan.  (use letter C)
    3. Euro.          (use letter E)

    """)
    if user_convert_choice == "I" or user_convert_choice == "i":
        indian_rupees = int(user_amount) * 0.48171
        print(f"{user_amount} PKR is {indian_rupees} Rs.")
    elif user_convert_choice == "C" or user_convert_choice == "c":
        china_yuan = int(user_amount) * 0.042300
        print(f"{user_amount} PKR is {china_yuan} Yuan.")
    elif user_convert_choice == "E" or user_convert_choice == "e":
        euro = int(user_amount) * 0.0054100
        print(f"{user_amount} PKR is {euro} Euro.")
    else:
        print("You have not entered a valid choice. The program will restart and enter a valid choice this time.")

# This section covers the conversion of USD to other currencies.


elif user_usd_pkr.upper() == "U":
    user_convert_choice = input("""Would you like to convert to -

    1. Indian Rupees. (use letter I)
    2. China's Yuan.   (use letter C)
    3. Euro.          (use letter E)

    """)

# indentation was missing here:
    if user_convert_choice == "I" or user_convert_choice == "i":
        indian_rupees = int(user_amount) * 73.27
        print(f"{user_amount} USD is {indian_rupees} Rs.")
    elif user_convert_choice == "C" or user_convert_choice == "c":
        china_yuan = int(user_amount) * 6.44
        print(f"{user_amount} USD is {china_yuan} Yuan.")
    elif user_convert_choice == "E" or user_convert_choice == "e":
        euro = int(user_amount) * 0.82
        print(f"{user_amount} USD is {euro} Euro.")

顺便说一句,您可以使用 user_usd_pkr.upper() == "U",这样您就不必检查 "U""u"