Question

In: Computer Science

Hello, trying to create a three way currency conversion on python. One of the things I...

Hello, trying to create a three way currency conversion on python. One of the things I want to do is create an error message whenever there is an incorrect currency entered. So far, I have managed to display the error message in these two cases: when both From and To currencies are incorrect, and when only the From currency is incorrect. What can I do so that my program can also display a message when the To currency is only incorrect.

money = float(input("Please enter the From amount to be converted: "))
fromCurrency = input("Please enter the From currency (USD, EUR, or JYP): ")
toCurrency = input("Please enter the To currency (USD, EUR, or JYP): ")

## conversions taken as of 9/26/19
# 1 USD = .91 EUR (1.1 factor), 1 USD = 107.63 JYP (.00929 factor), 1 EUR = 117.81 JYP (.0084 factor)
usdEur = money / 1.1
eurUsd = money * 1.1
usdJyp = money / .00929
jypUsd = money * .00929
eurJyp = money / .00848
jypEur = money * .00848

# Conversion from USD to other currencies
if fromCurrency.lower() == ("usd"):
if toCurrency.lower() == ("eur"):
result = usdEur
if toCurrency.lower() == ("jyp"):
result = usdJyp
print(money, fromCurrency, "equals", result, toCurrency)

elif fromCurrency.lower() == ("eur"):
if toCurrency.lower() == ("usd"):
result = eurUsd
if toCurrency.lower() == ("jyp"):
result = eurJyp
print(money, fromCurrency, "equals", result, toCurrency)

elif fromCurrency.lower() == ("jyp"):
if toCurrency.lower() == ("usd"):
result = jypUsd
if toCurrency.lower() == ("eur"):
result = jypEur
print(money, fromCurrency, "equals", result, toCurrency)

else: #does not work when 'to' currency is wrong
print("Error! Currency not available for conversion")

Solutions

Expert Solution

Thanks for the question, Below is the a much cleaner code. Instead of doing lower() every time, do it when you read the currency from the user to upper.

There can be 6 conversions and 1 valid conversion when the from and to currency are same so total 7 valid conversions, anything else is an invalid conversion and the last else statement takes care of that.

Here is the updated code. Hope it solves your problem.

thank 

Do comment incase you have any doubts, will repsond.

==============================================================================

money = float(input("Please enter the From amount to be converted: "))
fromCurrency = input("Please enter the From currency (USD, EUR, or JYP): ").upper()
toCurrency = input("Please enter the To currency (USD, EUR, or JYP): ").upper()

## conversions taken as of 9/26/19
# 1 USD = .91 EUR (1.1 factor), 1 USD = 107.63 JYP (.00929 factor), 1 EUR = 117.81 JYP (.0084 factor)
usdEur = money / 1.1
eurUsd = money * 1.1
usdJyp = money / .00929
jypUsd = money * .00929
eurJyp = money / .00848
jypEur = money * .00848

if fromCurrency == toCurrency and (fromCurrency == 'USD' or fromCurrency == 'EUR' or fromCurrency == 'JYP'):
    print(money, fromCurrency, "equals", money, toCurrency)
elif fromCurrency == 'USD' and toCurrency == 'EUR':
    print(money, fromCurrency, "equals", usdEur, toCurrency)
elif fromCurrency == 'USD' and toCurrency == 'JYP':
    print(money, fromCurrency, "equals", usdJyp, toCurrency)
elif fromCurrency == 'EUR' and toCurrency == 'USD':
    print(money, fromCurrency, "equals", eurUsd, toCurrency)
elif fromCurrency == 'EUR' and toCurrency == 'JYP':
    print(money, fromCurrency, "equals", eurJyp, toCurrency)
elif fromCurrency == 'JYP' and toCurrency == 'USD':
    print(money, fromCurrency, "equals", jypUsd, toCurrency)
elif fromCurrency == 'JYP' and toCurrency == 'EUR':
    print(money, fromCurrency, "equals", jypEur, toCurrency)
# if the above currency dont match then the from and to currency are incorrect
else:
    print("Error! Currency not available for conversion")


Related Solutions

hello, I need a python program for a drone to follow keyboard commands - Create a...
hello, I need a python program for a drone to follow keyboard commands - Create a simple single obstacle environment and program the drone to navigate through it - Create a more advanced obstacle environment (3+ obstacles) and program the drone to navigate through it
This is in Python, I am trying to create a new window to print the data...
This is in Python, I am trying to create a new window to print the data to screen, it prints fine in the terminal. I know this is a small segment of my code and its the only portion I am having a problem with. I was wondering if anyone can see what my problem may be. The current issue is NameError: name 'fltVal' is not defined and AttributeError: 'PressureGUI' object has no attribute 'r_count' Any help would be appreciated...
I am trying to write a python code to create a function that returns all possible...
I am trying to write a python code to create a function that returns all possible dimensions (reshaping) for an input matrix, can someone explains the math and write a python code with explaining all the steps and give me a test cases, and please explain if there is more than one approach. so if we have 1*8 matrix we should return 1*8, 2*4,4*2,8*1 def reshaping(M) should return a n ORDERED list AND the all possible reshaped matrices
In python I have a linked list. I want to create one function that takes in...
In python I have a linked list. I want to create one function that takes in one parameter, head. In the function, cur = head and next_cur = head.next. I want to return head and next_cur, except at the end of the function they will return alternating values from head. For example, if the these are the values in the linked list: 2, 3, 5, 7, 11 after the function head should return: 2, 5, 11 and next_cur should return:...
Hello! I'm trying to work on a python lab in my class, and we basically have...
Hello! I'm trying to work on a python lab in my class, and we basically have to make this method play_interactive Now for the fun bit. The function play_interactive will take just one argument --- the length of patterns to use as keys in the dictionary --- and will start an interactive game session, reading either '1' or '2' from the player as guesses, using the functions you wrote above and producing output as shown in the sample game session...
Hello, I have this question that I have been trying to solve for days. I am...
Hello, I have this question that I have been trying to solve for days. I am always able to solve the first part of the question, but I am never able to get the second part. Please help me understand how to set up this problem so I am able to solve it! Q: Let's revisit the banked curves from earlier to see another reason they are useful.We are building a road, and at one place we need to make...
Hello, I have been trying to answer this question for the last hour and I am...
Hello, I have been trying to answer this question for the last hour and I am still struggling could someone help me? The deadline is in 1hour! Perform an analysis of variance on the following data set. Do this by answering the questions below. Group 1 Group 2 Group 3 82 87 97 91 90 99 93 91 104 94 99 105 94 101 106 95 115 109 99 118 110 101 114 103 117 105 121 106 121 106...
Hello! Html question. I am trying to add a background to a page. How would i...
Hello! Html question. I am trying to add a background to a page. How would i do this? I have tried using a jpg. The code is posted below. i used the background-image: url function. Any help of different ways to do this would be appreciated! Thank you! CODE: <!DOCTYPE html> <html> <head> <style> ul { list-style-type: none; margin: 0; padding: 0; overflow: hidden; background-color: #333; } li { float: left; } li a { display: block; color: white; text-align:...
Python Coding Question (Data Science) Hello I am having difficulty writing a code in python to...
Python Coding Question (Data Science) Hello I am having difficulty writing a code in python to do a specific task. I have two text files, Positive.txt and Practice_forhw1.txt. I want to write a script that will check if any words in Practice_forhw1.txt match any of the words in Positive.txt then the word would get replaced with a "+1" in the Practice_forhw1.txt and then print out the Practice_forhw1.txt.  (i.e if the word "happy" is in both Positive.txt and Practice_forhw1.txt then I want...
Hello, I am trying to write a C++ program that will do the following: Use the...
Hello, I am trying to write a C++ program that will do the following: Use the STL stack container in a program that reads a string, an arithmetic expression to be exact, one character at a time, and determines if the string has balanced parenthesis – that is, for each left parenthesis there is exactly one matching right parenthesis later in the string.                         Use the following strings to test your program. A+ B - C A * B / (C...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT