Starting from:
$35

$29

Programming Project 03 Solution

Assignment Overview
This assignment will give you more experience on the use of strings and iterations.

The goal of this project is to use Google’s currency converter API to convert currencies in Python and display the result to user by processing the returned results.

Assignment Background

The acronym API stands for “Application Programming Interface”. It is usually a series of functions, methods or classes that supports the interaction of the programmer (the application developer, i.e., you) with some particular program. Google has several API's available for anyone to use. To interact with Google’s currency converter API, you have to create a GET request which is basically a URL and some values inserted in specific places. Our goal is to prompt the user for an amount, original currency and target currency, then setup the URL and request Google currency converter to convert it. We then process the returned value to extract the result, then display it to the user.

Project Description

Your program must meet the following specifications:
    1. At program start, prompt the user for the original currency.
    2. Then prompt the user for the target currency.

    3. Next prompt for the amount which must be an int.
    4. Setup the URL as explained below and send the request.
    5. Convert the returned value to string, and then process it to find the converted value.
    6. Display the result to the user.
    7. Prompt the user if they want to repeat.

Set up URL
A sample URL is https://finance.google.com/finance/converter?a=100&from=USD&to=EUR
You can put that link in a browser and see how Google currency converter converts 100 USD to EUR (convert $100 US to Euros). To change the amount or the currencies, you can simply replace those values in the URL by the values entered by the user. For this project, you can assume the user’s currency input is always correct. You will need to check that the amount to be converted is correct.

The URL needs to be a string for use in the urlopen(url) call below. You need to construct a URL string that has a value (such as 100 in sample URL above), the currency you are converting from (such as USD in the sample URL above) and the currency you are converting to (such as EUR in the sample URL above). You will be prompting for those three values so you need to insert them into your URL string. How do you do that? In Python we have the string format statement described in Section 4.4 of the text. Usually it is use inside a print()function, but it doesn’t have to be. Consider this code:

some_str = 'XYZ'

some_int = 123
a_str = "abc{:d}def{:s}".format(some_int,some_str)
print(a_str)    # prints abc123defXYZ

Process Results

Your project should start by importing urllib.request. You can use

response = urllib.request.urlopen(url) to do the conversion. Then read the response and convert it to string by result = str(response.read()). The result should look something like this:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">\n<html>\n<head>\n<title>Cur rency Converter - Google Finance</title>\n<meta name="Description" content="Get real-time stock quotes & charts, financial news, currency conversions, or track your portfolio with Google Finance.">

…………(very long html response)…………
<div id=currency_converter_result>100 USD = <span class=bld>81.5500
EUR</span>\n<input type=submit value="Convert">\n</div>\n<input

type=hidden name=meta
value=ei&#61;lJZhWoDJO4HLjAH31aWoBA>\n</form>\n</body>\n</div>\n</ht
ml>\n'

To find the converted value in the result string, you can find <span class=bld> that appears right before the value we are looking for and </span> that appears right after that (Hint: we said find as a hint to use the string find() method on the result string). You can then extract the value by slicing the result string using the indices you found.

Input Value Checking
The value input must be an integer. In a non-integer is input, you must re-prompt for the value (not re-prompt for the currency names). Hint: use a while loop (because you don’t know how many times an incorrect value will be input, and the string isdigit() method is useful to checking if an input string is all digits, i.e. is an integer.

Output Value
The value output must be a decimal with two decimal places.

Assignment Deliverable

The deliverable for this assignment is the following file:

proj03.py – the source code for your Python program

Be sure to use the specified file name and to submit it for grading via the Mimir before the project deadline.

Assignment Notes

    1. To clarify the project specifications, sample output is appended to the end of this document.
    2. Items 1-7 of the Coding Standard will be enforced for this project.

    3. Use a while loop with a Boolean that keeps looping as long as the user wants to convert currencies. Remember to prompt at the end (a.k.a. “bottom”) of the loop.

    4. There will be no error checking of currency names in this assignment, e.g. USD, EUR. Also,
all currency names are exactly three characters.
    5. The user can input the strings using any combinations of lower and upper case letters. You should convert them to upper case for use in the URL and in output of your results.
    6. You may not use advanced data structures such as lists, tuples, dictionaries or classes for this project—no credit if you do. (Note that at this point you likely don’t recognize those terms because they are covered later in the course.)
    7. We provide a file strings.txt that has the strings used in our code so you can match the expected output in the Mimir tests.

Test Cases

Note that the exchange rates are changing constantly, so if the value is slightly off, that’s probably the reason.

Test 1

What is the original currency? CAD

What currency do you want to convert to? EUR

How much do you want to convert (int)? 15

150 CAD is 9.82 EUR

Do you want to convert another currency? yEs

What is the original currency? usd

What currency do you want to convert to? gbp

How much do you want to convert (int)? 12

12 USD is 8.66 GBP

Do you want to convert another currency? nO

Test 2

What is the original currency? eur

What currency do you want to convert to? usd

How much do you want to convert (int)? 50.5
The value you input must be an integer. Please try again.

How much do you want to convert (int)? invalidinput
The value you input must be an integer. Please try again.

How much do you want to convert (int)? 78.00
The value you input must be an integer. Please try again.
How much do you want to convert (int)? 5 5 EUR is 6.11 USD

Do you want to convert another currency? yes

What is the original currency? cad

What currency do you want to convert to? usd

How much do you want to convert (int)? 7

7 CAD is 5.60 USD

Do you want to convert another currency? no

Test 3

This test is a blind test. You do not get to see input or output.

Grading Rubric

5 pts

Coding Standard
(descriptive comments, mnemonic identifiers, format, etc...)

    5 pts Test 1

    5 pts Test 2

    5 pts Test 3

More products