214k views
5 votes
This problem is about Python modules.

Crate a module currency, which includes the following three functions that do currency conversions:

to_euro(dollar): This function receives US Dollar as an argument and converts it to Euro. 1 US Dollar = 0.81 Euro. Return Euro.

to_yen(dollar): This function receives US Dollar as an argument and converts it to Japanese Yen. 1 US Dollar = 106.45 Yen. Return Yen.

to_peso(dollar): This function receives US Dollar as an argument and converts it to Mexican Peso. 1 US Dollar = 18.58 Peso. Return Peso.

Store these three functions in a file named currency.py.

Create a file for the main module. Name the file lab12P2.py.

Define a main function in the main module to do the following:

Ask the user to choose a foreign currency: Euro, Japanese Yen or Mexican Peso.

Write a loop to validate user input. If an invalid choice is made, display an error message and ask the user to choose a foreign currency again until the choice is valid.

Ask the user to enter US dollar amount. Write a loop to validate user input. If the US dollar amount is negative, display an error message and ask the user to reenter it until it is non-negative.

Call one of the three functions in the currency module to convert US dollar to the foreign currency chosen by the user

Receive and display the converted foreign currency

The following is an example.

Converting US Dollar to a foreign currency.

Enter 1 for Euro, 2 for Japanese Yen, 3 for Mexican Peso: 4

Error: Invalid Choice

Enter 1 for Euro, 2 for Japanese Yen, 3 for Mexican Peso: 5

Error: Invalid Choice

Enter 1 for Euro, 2 for Japanese Yen, 3 for Mexican Peso: 2

Enter US Dollar: -100

Error: US Dollar cannot be negative.

Enter US Dollar: -200

Error: US Dollar cannot be negative.

Enter US Dollar: 100

It is converted to 10645.0 Yen

User Spong
by
4.8k points

1 Answer

4 votes

Answer:

def to_euro(dollar):

return float(dollar)*0.81;

def to_yen(dollar):

return float(dollar)*106.45;

def to_peso(dollar):

return float(dollar)*18.58;

def main():

while(1):

x=input("Enter 1 for Euro, 2 for Japanese Yen, 3 for Mexican Peso: ");

x=int(x);

if(x==1 or x==2 or x==3):

break;

else:

print("Error: Invalid choice");

while(1):

y=input("Enter US Dollar: ");

y=float(y);

if(y<0):

print("Can't be negetive");

else:

break;

if(x==1):

print("Its is converted to ",to_euro(y)," Euro" );

elif(x==2):

print("Its is converted to ",to_yen(y)," Yen" );

else:

print("Its is converted to ",to_peso(y)," Peso" );

main()

Step-by-step explanation:

User Blueyed
by
4.2k points