Question

Hi, trying to make a converter but is running into an issue of displaying an error...

Hi, trying to make a converter but is running into an issue of displaying an error message (at the bottom of the program) when currencies are entered incorrectly. I was suggested after adding result = 0, I should test for result == 0. With only making necessary changes can you explain what this means to my program and how?


result = 0
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")


Homework Answers

Answer #1

Changes made to the program:

  1. Removed the print statement for printing result from all if blocks and added at the end of the program. One print is enough instead of three.
  2. Added If else condition to check if the result is 0, to print the error message.
  3. Also added money!=0 condition for displaying error message. This is required because if money entered is 0, and though both fromCurrency and toCurrency entered are valid, program displays the error message.
  4. To avoid above case, added another if condition to check if money==0. If money==0, "Money entered is 0" is printed instead of error message.

Program:

Note: Program is explained using comments in the code.

Code:

#initialise result to 0
result = 0

#input money from user
money = float(input("Please enter the From amount to be converted: "))

#input from currency
fromCurrency = input("Please enter the From currency (USD, EUR, or JYP): ")

#input to currnecy
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)

#If conversion is from USD to EUR
usdEur = money / 1.1

#If conversion is from EUR to USD
eurUsd = money * 1.1

#If conversion is from USD to JYP
usdJyp = money / .00929

#If conversion is from JYP to USD
jypUsd = money * .00929

#If conversion is from EUR to JYP
eurJyp = money / .00848

#If conversion is from JYP to EUR
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
# Conversion from EUR to other currencies
elif fromCurrency.lower() == ("eur"):
   if toCurrency.lower() == ("usd"):
       result = eurUsd
   if toCurrency.lower() == ("jyp"):
       result = eurJyp
# Conversion from JYP to other currencies
elif fromCurrency.lower() == ("jyp"):
   if toCurrency.lower() == ("usd"):
       result = jypUsd
   if toCurrency.lower() == ("eur"):
       result = jypEur

#displays error message if any of fromCurrency or toCurrency is invalid

#If any of the fromCurrency or toCurrency is invalid(i.e not USD or EUR or JYP)
#Then the if conditions above wont satisfy and the result will be same as initialised
# that is 0.

# If money entered is 0 then though fromCurrency and toCurrency entered are
# valid result will be 0.

#So the condition to check for displaying error meassage is result==0 AND money!=0
if(result==0 and money!=0):
   print("Error! Currency not available for conversion")
elif(money==0):
   print("Money entered is 0")
else:
   print(money, fromCurrency, "equals", result, toCurrency)


Output:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Hi I need to make a hangman game for school task and it is no an...
Hi I need to make a hangman game for school task and it is no an ordinary game. It has to incorporate the pictures and everytime something wrong is guessed it has to clock over the pictures. The pictures are: hangman_pics = [''' +---+ | | | | | | =========''', ''' +---+ | | O | | | | =========''', ''' +---+ | | O | | | | | =========''', ''' +---+ | | O | /| |...
convert this code to accept int value instead of float values using python. Make sure to...
convert this code to accept int value instead of float values using python. Make sure to follow the same code. do not change the steps and make sure to point to what code you replaced. make sure to have 2 files Method:----------------------- #define a python user difined method def get_float_val (prompt): is_num = False str_val = input (prompt) #prming read for our while #while is_num == False: (ignore this but it works) old school while not is_num: try: value =...
Create a program called Currency Conversion. This will convert one currency to other currencies (for example...
Create a program called Currency Conversion. This will convert one currency to other currencies (for example CAD to GBP, Euro and USD).  The program should prompt user to choose currency in hand and currency required. It should also ask the user to enter amount (currency value).  The conversion rate is fixed as mentioned below. You should store the exchange rate as final values.  Use Scanner for input and printf for formatting decimals (rounded to two decimal places)...
So, i have this code in python that i'm running. The input file is named input2.txt...
So, i have this code in python that i'm running. The input file is named input2.txt and looks like 1.8 4.5 1.1 2.1 9.8 7.6 11.32 3.2 0.5 6.5 The output2.txt is what i'm trying to achieve but when the code runs is comes up blank The output doc is created and the code doesn't error out. it should look like this Sample Program Output 70 - 510, [semester] [year] NAME: [put your name here] PROGRAMMING ASSIGN MENT #2 Enter...
I need to add two trivial functions to the following code. I am having an issue...
I need to add two trivial functions to the following code. I am having an issue with what i may need to change in the current code to meet the requirements. I am already displaying the bank record but now using a function Here are some examples of what i can add to the code 1. Obtain opening (current) balance. 2. Obtain number the number of deposits. 3. Obtain number of withdrawals. 4. Obtain deposit amounts. 5. Obtain withdrawal amounts....
Python Blackjack Game Here are some special cases to consider. If the Dealer goes over 21,...
Python Blackjack Game Here are some special cases to consider. If the Dealer goes over 21, all players who are still standing win. But the players that are not standing have already lost. If the Dealer does not go over 21 but stands on say 19 points then all players having points greater than 19 win. All players having points less than 19 lose. All players having points equal to 19 tie. The program should stop asking to hit if...
C++ PROGRAM SPECIFICATION For the assignment, we will use the previous assignment’s program that determines whether...
C++ PROGRAM SPECIFICATION For the assignment, we will use the previous assignment’s program that determines whether a college class room is in violation of fire law regulations regarding the maximum room capacity and add more logic to that program. We will need to make the following enhancements… The program should now determine the number of classes, and should do so by generating a unique, random number. Replace taking user input for the number of rooms with a computer generated number...
Program will allow anywhere between 1 and 6 players (inclusive). Here is what your output will...
Program will allow anywhere between 1 and 6 players (inclusive). Here is what your output will look like: Enter number of players: 2 Player 1: 7S 5D - 12 points Player 2: 4H JC - 14 points Dealer: 10D Player 1, do you want to hit? [y / n]: y Player 1: 7S 5D 8H - 20 points Player 1, do you want to hit? [y / n]: n Player 2, do you want to hit? [y / n]: y...
Base Conversion One algorithm for converting a base 10 number to another base b involves repeatedly...
Base Conversion One algorithm for converting a base 10 number to another base b involves repeatedly dividing by b. Each time a division is performed the remainder and quotient are saved. At each step, the dividend is the quotient from the preceding step; the divisor is always b. The algorithm stops when the quotient is 0. The number in the new base is the sequence of remainders in reverse order (the last one computed goes first; the first one goes...
[PART ONE OF PROJECT, ALREADY COMPLETED] An accumulator is a primitive kind of calculator that can...
[PART ONE OF PROJECT, ALREADY COMPLETED] An accumulator is a primitive kind of calculator that can evaluate arithmetic expressions. In fact, the Arithmetic-Logic Unit (ALU) of the rst computers was just an accumulator. An arithmetic expression, as you know, consists of two kinds of tokens: operands and operators All our operands will be (float) numbers and for a start, we shall use only two operators: + (plus) and - (minus) A sample run of the program would look like this....
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT