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 =...
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...
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....
Getting the following errors: Error 1 error C2436: '{ctor}' : member function or nested class in...
Getting the following errors: Error 1 error C2436: '{ctor}' : member function or nested class in constructor initializer list on line 565 Error 2 error C2436: '{ctor}' : member function or nested class in constructor initializer list on line 761 I need this code to COMPILE and RUN, but I cannot get rid of this error. Please Help!! #include #include #include #include using namespace std; enum contactGroupType {// used in extPersonType FAMILY, FRIEND, BUSINESS, UNFILLED }; class addressType { private:...
#Linked Lists and Classes #C++ Hi, please use singly linked list method to do this question....
#Linked Lists and Classes #C++ Hi, please use singly linked list method to do this question. Thank you! Here’s the contents of a file called example.cpp: // example.cpp #include "LinkedList.h" #include <iostream> #include <string> using namespace std; int main() { cout << "Please enter some words (ctrl-d to stop):\n"; LinkedList lst; int count = 0; string s; while (cin >> s) { count++; lst.add(remove_non_letters(s)); } // while cout << "\n" << count << " total words read in\n"; cout <<...