Python Programming Experiment #1

 

Overview: Currency conversion task

Assuming that one US dollar can exchange for 33 Taiwanese dollars right now, your program will help people figure out the currency conversion task described below. The task is to ask the user to tell you the numbers of pennies, nickels, dimes, and quarters the user has respectively in terms of US currency, and then reports the following information to the user:

·                the total amount of money entirely in terms of US cents only,

·                the total amount of money in terms of US dollars and cents in the standard format,

·                the equivalent total entirely in terms of Taiwanese cents only, and

·                the equivalent total in terms of Taiwanese dollars and cents in the standard format.

 

Example 1: If the user tells the program that he/she has 3 pennies, 2 nickels, 3 dimes, and 4 quarters. The program should calculate and report that

·                the total is 143 US cents,

·                it equals 1 US dollar and 43 US cents,

·                it can be exchanged for 4719 Taiwanese cents, and

·                it equals 47 Taiwanese dollars and 19 Taiwanese cents.

 

Variables, integer division, and modulo operation:

In your program, you should use several variables for storing and processing (i) the information of the numbers of different kinds of coins and the amount of dollars and cents in different currencies and (ii) the total amount of money entirely in terms of US cents and the equivalent amount entirely in Taiwanese cents. 

You can first calculate the total amount of money entirely in terms of US cents based on the numbers of different kinds of coins the user has, store the result in a corresponding variable, and then use / and %, (the integer division operator and the modulo operator) to help you determine the amount in the standard format of dollars and cents in US currency as shown in the following example.

 

Example 2: Consider what we have in Example 1. It is easy to calculate that all together we have 143 US cents in total. Apply floor division to divide 143 by 100 and you get 1.  And 143 % 100 gives you 43. Therefore, we know the amount of money in the standard format is 1 US dollar (the result of the integer division 143 / 100) and 43 US cents (the result of the modulus operation 143 % 100).

You can also multiply the total amount entirely in US cents by 33 to get the equivalent of the total amount entirely in Taiwanese cents and then proceed in a similar fashion mentioned above to figure the amount in the standard format of dollars and cents in Taiwanese currency.

 

Example 3: Consider what we have in Example 2. All together we have 143 US cents in total. Multiply 143 by 33 and you get 4719.  . Apply floor division to divide 4719 by 100 and you get 47.  And 4719 % 100 gives you 19. Therefore, we know it equals 47 Taiwanese dollars (the result of the integer division 4719 / 100) and 19 Taiwanese cents (the result of the modulus operation 4719 % 100).

 

 

Stage A (testing in the interactive mode): 

Invoke IDLE Python GUI to work with Python under the interactive mode, and try to issue instructions in the Python programming language to do some of the following things:

a)     Print a message to ask the user to enter the number of pennies.

b)    Use the input function in Python 3 (or raw_input in Python 2) to read the number of pennies as a string and assign it into a variable inputString

c)     Use the int function to get the integer value from the inputString and assign the numerical value into another variable pennies

d)    Use similar steps as in a, b, and c above to read and store the number of nickels, the number of dimes, and the number of quarters in three other variables nickels, dimes, and quarters.

e)     Calculate the total amount entirely in US cents based on the values stored in pennies, nickels, dimes, and quarters and assign the result into another variable TotalUsCents. In other words, TotalUsCents = pennies+ 5*nickels+ 10*dimes+ 25*quarters. Print out a message about this amount in US cents.

f)     Print out another  message in the standard format stating the amount as equivalent to TotalUsCents/100 US dollars and TotalUsCents%100 US cents based on the values stored in TotalUsCents

g)    Calculate the total amount in Taiwanese cents based on the exchange rate (1 US dollar to 33 Taiwanese dollars) and the value stored in TotalUsCents and then assign the result into another variable TotalTaiwanCents In other words, TotalTaiwanCents = 33* TotalUsCents. Print out a message about this amount in Taiwanese cents.

h)    Print out another  message in the standard format stating the amount as equivalent to TotalTaiwanCents/100 US dollars and TotalTaiwanCents%100 US cents based on the values stored in TotalTaiwanCents.

 

 

Stage B (writing down the program): 

If you are able to correctly conduct temperature conversion throughout all the steps in Stage A, you can now go to File => New Window under IDLE Python GUI to invoke a new editing Window. Write down the Python instructions you used in Stage A under this new Window and save them in file, say program2.py.

 

Stage C (running the program): 

Under the new window, go to Run => Run Module to run the whole program you have written in Stage B. Make sure your program can do the currency conversion correctly. For example, Example 1: If the user tells the program that he/she has 3 pennies, 2 nickels, 3 dimes, and 4 quarters. The program should calculate and report that

(i)             the total is 143 US cents,

(ii)           it equals 1 US dollar and 43 US cents,

(iii)         it can exchange for 4719 Taiwanese cents, and

(iv)          that equals 47 Taiwanese dollars and 19 Taiwanese cents.