SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Y Daniel Lang
Chapter:
.selections
Exercise:
Programming Excercises
Question:18 | ISBN:978013274719 | Edition: 6

Question

(Financials: currency exchange) Write a program that prompts the user to enter the currency exchange rate between U.S. dollars and Chinese Renminbi (RMB). Prompt the user to enter 0 to convert from U.S. dollars to Chinese RMB and 1 for vice versa. Prompt the user to enter the amount in U.S. dollars or Chinese RMB to convert it to Chinese RMB or U.S. dollars, respectively. Here are some sample runs:

 

Enter the exchange rate from dollars to RMB: 6.81
Enter 0 to convert dollars to RMB and 1 vice versa: 0
Enter the dollar amount: 100
$100.0 is 681.0 yuan
 

Enter the exchange rate from dollars to RMB: 6.81
Enter 0 to convert dollars to RMB and 1 vice versa: 1
Enter the RMB amount: 10000
10000.0 yuan is $1468.43

 

Enter the exchange rate from dollars to RMB: 6.81
Enter 0 to convert dollars to RMB and 1 vice versa: 5
Incorrect input
 

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

print("Enter 0 to convert dollars to RMB and 1 vice versa: ")
option = int(input())
#Prompt the user to enter the amount in USD or RMB
#to convert it to RMB or USD respectively

if(option==0):
    print("Enter the dollar amount: ")
    amount = float(input())
    print("$  {}   is  {}   yuan".format(amount,(amount * rate)))
elif(option==1):
    print("Enter the RMB amount: ")
    amount =float(input())
    rmb=((amount * 100) / rate) / 100.0
    print("{}   yuan is $ {:.2f}".format(amount,rmb))
elif(option>1):
    print("Incorrect input")

Output:

Enter the exchange rate from dollars to RMB: 
6.81
Enter 0 to convert dollars to RMB and 1 vice versa: 
1
Enter the RMB amount: 
10000
10000.0   yuan is $ 1468.43

0 0

Discussions

Post the discussion to improve the above solution.