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



所以我试图制作一个货币转换器应用程序。它询问用户的金额,并询问输入的金额是巴基斯坦卢比还是美元,并提供转换为印度卢比、人民币还是欧元的选项。然后,它将金额转换为选定的货币。我遇到的问题是,当我选择将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.")

问题在于缩进。最低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"了。

最新更新